如何解决用php中的代码构建Blog Archives块?
| 我是php的新手,我只能读写一些数据到mysql数据库和一些基本的php知识。现在,有一个博客程序。我写了一些文章。我想用自己的代码构建Blog Archives块。但我什至不知道如何开始? Blog Archives块样式如下:2011 year may month
2011 year april month
2011 year march month
....
希望有人可以给我一些提示。首先我应该做.....最后解决问题。谢谢。
ps:首先我知道我应该对数据库进行研究。现在,但是我不知道我需要多少字段。
我发现文章的创建时间为this1ѭ
解决方法
编辑:根据您的评论,我了解您在数据库中存储了时间戳。 Month是一个mysql datetime函数(请阅读这篇文章),但是现在将不再使用。
您应该使用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;
}
,如果您的博客发布位于一个表(blog?)中,请创建具有完全相同结构的存档表(blog_BAK?),并使用以下语法创建备份:
SELECT INTO
http://www.w3schools.com/sql/sql_select_into.asp
此后,每次添加/更改博客时,都将数据依次插入两个表中。
,也许改变您的数据模型?向数据库添加一个名为““ archived \”的新字段,并将默认值设置为int(0)(如果要将其标记为存档),将其设置为1。这样一来,您仍然可以将所有内容都放在一个地方,而只需读取标志以确定它是否是存档。这是数据库的示例。
blog_id | date | title | article | archived
1 | DATE | TITLE | ARTICLE | 1 <- this is archived
2 | DATE | TITLE | ARTICLE | 0 <- this is not
希望有帮助...
,测试完整的构建博客样式档案的示例
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>