php数组与mysql数据库协同工作


php array working with mysql database?

我对php和mysql还很陌生,我已经在我的网站上为我的新闻文章和事件等构建了一个基本的CMS,我需要一个按钮来添加一个新的年份类别。

我目前有一个2011年文章的表单按钮,所以当用户点击它时,我的新闻页面将获得2011年的值,它是隐藏的,只显示2011年的文章。

我想也许可以使用一个数组,这样当我点击添加新类别时,它只会在2011年增加1,我可以获取数组值,并使用它们——当我生成第二个表单/第三个表单时,如何添加到我存储隐藏表单值的位置。。。。等

我希望这是有道理的,如果有人知道一种更简单的方法,我愿意接受各种想法。这是我目前使用的代码


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

我建议您将样式放在另一个文件中以保持整洁。试试这个:


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