使用php中的代码构建Blog Archives块?

时间:2011-05-25 14:11:57

标签: php

我是php的新学习者,我只能读取和写入一些数据到mysql数据库和一些基本的PHP知识。现在,有一个博客程序。我写了一些文章。我想用自己的代码构建一个Blog Archives块。但我甚至不知道如何开始? Blog Archives阻止样式:

2011 year  may month
2011 year  april month
2011 year  march month
....

期待有人可以给我一些提示。首先我应该这样做.....并且最后解决问题。谢谢。

ps:首先我知道我应该对数据库进行研究。现在,但我不知道我需要多少个字段。 我发现文章创建的时间为1305357473

4 个答案:

答案 0 :(得分:2)

编辑:从您的评论中我了解到您的数据库中存储了时间戳。 Month是一个mysql日期时间函数(读this),但现在不会使用。

您应该使用php来执行以下操作,请记住这是一个原始示例..并且需要您自己的一些详细输入。

while($loopTroughResultHere) {

   $oDateTime = new DateTime($aRow['created']);
   $iYear = $oDateTime->format("Y");
   $sMonth = $oDateTime->format("F");

   // Do some isset checking here, to see if the key year has been set already.
   $aPosts[$iYear][$sMonth]][] = $aRow;
}

答案 1 :(得分:1)

如果您的博客帖子在一个表(博客?)中,请创建具有完全相同结构的归档表(blog_BAK?)并使用以下语法创建备份:

SELECT INTO

http://www.w3schools.com/sql/sql_select_into.asp

此后,每次添加/更改博客时,都会依次将数据插入两个表中。

答案 2 :(得分:0)

也许更改您的数据模型?将新字段添加到名为“archived”的数据库中,并将默认设置为int(0),如果您希望将其标记为存档,则将其设置为1.那样您仍然可以在一个地方拥有所有内容,而您只需要阅读标志以确定它是否是存档。以下是DB的示例。

blog_id | date | title | article | archived
1       | DATE | TITLE | ARTICLE | 1         <- this is archived
2       | DATE | TITLE | ARTICLE | 0         <- this is not

希望有帮助...

答案 3 :(得分:0)

TEST full构建博客样式存档的示例

CSS文件

#Decor 
{
    width:200px;
}

#Decor, #Decor ul {
    list-style-type: none;
    margin-left:25px;
    margin-bottom:5px;
    padding-left:20px;
    cursor:pointer;
}

.handle { 
    background: transparent url( /images/tree-handle.png ) no-repeat left top; 
    display:block; 
    float:left;
    width:10px; 
    height:10px;
    cursor:pointer;
}

.closed { background-position: left top; }
.opened { background-position: left -10px; }

PHP文件

只需将css代码添加到php html文件

    <script src="scripts/jquery-1.11.1.min.js"></script>
    <script>
        $(function () {
            //start the tree in an autocollapsed state
            $('#Decor ul').hide(400);

            $('#Decor li').on('click', function (e) {
                e.stopPropagation(); // prevent links from toggling the nodes
                $(this).children('ul').slideToggle();
            });

            // This code opens all hyperlinks in a new window 
            // and avoids anchors
            $('#Decor a').not('[href="#"]').attr('target', '_blank');
        });
    </script>
</head>

<?php
define("DB_USER","");
define("DB_PASS","");
define("DB_HOST","");
define("DB_NAME","");

$link = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die(mysqli_errno());
$s="SELECT *,content_id,COUNT(content_id) AS itemCount FROM content_mast GROUP BY DATE_FORMAT(date_upload,'%Y') DESC";
$sql_result=mysqli_query($link,$s);
?>
<ul id="Decor">
<?php
while($row=mysqli_fetch_array($sql_result))
    {
    $datetime=strtotime($row['date_upload']);
    $tday = date("Y", $datetime);
    $count = $row['itemCount'];
    ?>
     <li class="level1"><?php echo "<u><strong>{$tday} ({$count})</strong></u><br>"; ?>
    <?php
        $s1="select * from content_mast where DATE_FORMAT(date_upload,'%Y')=$tday";
        $q=mysqli_query($link,$s1);
        while($month=mysqli_fetch_row($q))
        {
        ?>
            <ul>
               <li><a href="test.php?date=<?php echo $month[5]; ?>"><?php echo date("F",strtotime($month[5])); ?></a></li>
            </ul>
            <?php
        }
    echo "<br>";
    }
?>
</ul>
</html>