我正在尝试在我的网站上创建新闻档案。我写了这段代码:
$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
Month: 2013-05 - News Number: 3
Month: 2013-04 - News Number: 4
Month: 2013-03 - News Number: 3
我的问题是,如何在那个月之后的每个月显示新闻标题?例如:
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
答案 0 :(得分:1)
尝试使用此代码。
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']}";
}
}
答案 1 :(得分:0)
您可以在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>";
}
}
答案 2 :(得分:0)
使用PHP和MySQL创建年度存档
$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>";
}
}
关于输出图像。
<强>输出强>
答案 3 :(得分:0)
测试完整使用PHP和MySQL(如博客)
的年度存档示例<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>