1

I'm still pretty new to php and mysql, I've build a basic CMS for my news articles and events etc on my site and I need a button that will add a new year category.

I currently have a form button for 2011 articles so when a user clicks it my news page will get the value 2011 which is hidden and only show articles with the year 2011.

I was thinking of maybe using an array so that when I click add new category it just adds 1 to the year 2011 and I can grab the array values and use them some how to add to where I store my hidden form value when I generate a second form / third .... etc.

I hope that makes sense, if anyone knows an easier way to do this I'm open to ideas. heres the code I use currently


news.php

<form action="all-news.php" method="POST" name="editform">
    <input type="hidden" name="category" value="2013"/>
    <input type="hidden" name="item" value="1"/>
    <input type="submit" style="padding: 2px 15px;background-color:#C00; color:#FFF; margin:10px; font-size:18px; font-family:'Trebuchet MS', Arial, Helvetica, sans-serif" name="go" value="News Articles 2013">
    </form>

newslist.php

    $category = $_POST['category'];

        $query = "SELECT * FROM sitecontent WHERE $category = Post_Year ORDER BY Date DESC";
        $result = mysql_query($query);

        while($posts = mysql_fetch_array($result)){
            echo "<div>";
            echo "<h3>", $posts['Post_Title'], "</h3>", "<br/>";
            echo "<a href=\"delete.php?id=$posts[ID]\"> ", "<span style=\"cursor:pointer; padding: 2px 8px;background-color:#C00; color:#FFF; margin-right:10px;\">" , "Delete", "</span>" , "</a>";
            echo "<a href=\"edit.php?id=$posts[ID]\"> ", "<span style=\"cursor:pointer; padding: 2px 8px;background-color:#C00; color:#FFF; margin-right:10px;\">" , "Edit", "</span>" , "</a>";
            echo "</div><br/>";

        }

1 Answer 1

1

I recommend you put your styles in another file to keep things clean. Try this:


style.css

.button {
    padding: 2px 15px;
    background-color: #C00; 
    color: #FFF; 
    margin: 10px; 
    font-size: 18px; 
    font-family: 'Trebuchet MS', Arial, Helvetica, sans-serif;
}


news.php

<link rel="stylesheet" type="text/css" href="path/to/style.css">
<?php
$query = mysql_query("SELECT * FROM sitecontent ORDER BY Date DESC");
while($posts = mysql_fetch_array($query)) {
    if(!in_array($posts["Post_Year"], $desiredYears))
    {
        $desiredYears[count($desiredYears)] = $posts["Post_Year"];
    }
}

foreach($desiredYears as $currentYear)
{
    ?>
    <form action="all-news.php" method="POST" name="editform">
        <input type="hidden" name="category" value="<?php echo $currentYear; ?>"/>
        <input type="hidden" name="item" value="1"/>
        <input type="submit" name="go" class="button" value="News Articles <?php echo $currentYear; ?>">
    </form>
    <?php
}
?>
2
  • Thanks for that, so for my cms I want a button that says add category how would that then add to the array and database, I have a database field called Category in the news table and that'll be the year of post is there a way to get the different types of year from my table. Something like ... get(categories from news table){ if(Category is unique){ store in array... } } Commented Jan 7, 2012 at 20:17
  • Hello, I changed the news.php a bit in order to pull the years from the database and display them after. I believe this is what you are looking for. Commented Jan 7, 2012 at 21:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.