huddds
huddds

Reputation: 1045

php array working with mysql database?

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/>";

        }

Upvotes: 1

Views: 282

Answers (1)

Johnathan Croom
Johnathan Croom

Reputation: 78

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
}
?>

Upvotes: 1

Related Questions