使用PHP和MySQL创建每月存档
我正在尝试在我的网站上创建一个新闻档案。我写了这段代码:使用PHP和MySQL创建每月存档,php,mysql,while-loop,archive,Php,Mysql,While Loop,Archive,我正在尝试在我的网站上创建一个新闻档案。我写了这段代码: $sql_result = $db->query(" SELECT *,COUNT(id) AS itemCount FROM post GROUP BY DATE_FORMAT(date, '%Y-%m') DESC "); while ( $row = $db->get_row( $sql_result ) ) { $datetime = strtotime($row['date']); $tday = date
$sql_result = $db->query("
SELECT *,COUNT(id) AS itemCount
FROM post
GROUP BY DATE_FORMAT(date, '%Y-%m') DESC ");
while ( $row = $db->get_row( $sql_result ) ) {
$datetime = strtotime($row['date']);
$tday = date("Y-m", $datetime);
$count = $row['itemCount'];
echo "Month: {$tday} - News Number: {$count}<br>";
}
我的问题是,我怎样才能在那个月之后的每个月显示新闻标题?例如,类似这样的事情:
Month: 2013-06 - News Number: 4
-news number 1 for this month
-news number 2 for this month
-news number 3 for this month
-news number 4 for this month
Month: 2013-05 - News Number: 3
-news number 1 for this month
-news number 2 for this month
-news number 3 for this month
Month: 2013-04 - News Number: 4
-news number 1 for this month
-news number 2 for this month
-news number 3 for this month
-news number 4 for this month
Month: 2013-03 - News Number: 3
-news number 1 for this month
-news number 2 for this month
-news number 3 for this month
您可以在while循环中添加一个循环,如下所示
while ( $row = $db->get_row( $sql_result ) ) {
$datetime = strtotime($row['date']);
$tday = date("Y-m", $datetime);
$count = $row['itemCount'];
echo "Month: {$tday} - News Number: {$count}<br>";
/////////////////////////
// You can add a sql query here, as you added before while loop, to get each
// month's data on the basis of unique-id which you have in '$row'
/////////////////////////
for($i=1; $i<=$count; $i++)
{
echo "-news number {$i} for this month<br>";
}
}
尝试使用此代码
while ( $row = $db->get_row( $sql_result ) ) {
$datetime = strtotime($row['date']);
$tday = date("Y-m", $datetime);
$count = $row['itemCount'];
echo "Month: {$tday} - News Number: {$count}<br>";
$sql_result1 = $db->query("
SELECT newsTitle
FROM post
WHERE DATE_FORMAT(date, '%Y-%m')=DATE_FORMAT({$datetime}, '%Y-%m')");
while($row1 = $db->get_row( $sql_result1 ) ){
echo "News: {$row1['newsTitle']}";
}
}
使用PHP和MySQL创建年度存档
关于输出图像
输出使用PHP和类似MySQL的博客测试年度归档的完整示例
谢谢,但我如何才能显示该列表中每个月的所有新闻?我的意思是,我如何从MySQL获取当月的数据?我已经编辑了我的代码,我添加了一条评论,我希望你能得到它。您可以添加sql查询来获取while循环中每个月的数据。试试这个,它会起作用的。请检查我写的SQL语句中的列名。同一个问题有两个答案吗?请选择一个:-另一个响应有图像,输出什么都不做?请复习。
while ( $row = $db->get_row( $sql_result ) ) {
$datetime = strtotime($row['date']);
$tday = date("Y-m", $datetime);
$count = $row['itemCount'];
echo "Month: {$tday} - News Number: {$count}<br>";
$sql_result1 = $db->query("
SELECT newsTitle
FROM post
WHERE DATE_FORMAT(date, '%Y-%m')=DATE_FORMAT({$datetime}, '%Y-%m')");
while($row1 = $db->get_row( $sql_result1 ) ){
echo "News: {$row1['newsTitle']}";
}
}
$link = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die(mysqli_errno());
$s="SELECT *,COUNT(content_id) AS itemCount FROM content_mast GROUP BY DATE_FORMAT(date_upload, '%Y') DESC ";
$sql_result =mysqli_query($link,$s);
while ($row = mysqli_fetch_array($sql_result))
{
$datetime = strtotime($row['date_upload']);
$tday = date("Y", $datetime);
$count = $row['itemCount'];
echo "Month: {$tday} - News Number: {$count}<br>";
for($i=1; $i<=$count; $i++)
{
echo "-news number {$i} for this month<br>";
}
}
<link href="CSS/treeview.css" rel="stylesheet" />
<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="#"><?php echo date("M",strtotime($month[5]))."<br>"; ?></a></li>
</ul>
<?php
}
echo "<br>";
}
?>
</ul>
</html>