Php 按时间排序和按时间排序不为空
我的php+mysql代码Php 按时间排序和按时间排序不为空,php,mysql,Php,Mysql,我的php+mysql代码 //row mysql $row_news = "SELECT `id`,`title_name`,`create_time`,`title_img` FROM `news` WHERE `status` = '1' ORDER BY `create_time` DESC,`title_img` DESC"; $stmt = $pdo->prepare($row_news); $stmt->execute(); $row_news = $stmt->
//row mysql
$row_news = "SELECT `id`,`title_name`,`create_time`,`title_img` FROM `news` WHERE `status` = '1' ORDER BY `create_time` DESC,`title_img` DESC";
$stmt = $pdo->prepare($row_news);
$stmt->execute();
$row_news = $stmt->fetchAll(PDO::FETCH_ASSOC);
//Gropu by DATE row
foreach ($row_news as $key => $value) {
$date = date("Y-m-d", $value["create_time"]);
$row_news[$key]['date'] = $date;
}
foreach ( $row_news as $value ) {
$group[$value['date']][] = $value;
}
我想展示一下
======================
2015-10-20
======================
[img] title-1......
[img] title-2......
[img] title-3......
title-4.....
title-5.....
title-6.....
======================
2015-10-19
======================
[img] title-1......
[img] title-2......
title-3.....
但是,这个sql不能做到这一点
我需要按日期分组来划分内容
创建时间
======================
2015-10-20
======================
[img] title-1......
title-4.....
[img] title-2......
[img] title-3......
title-5.....
title-6.....
======================
2015-10-19
======================
[img] title-1......
title-3.....
[img] title-2......
html演示:
有什么建议吗?你真正的问题是你的疑问。。。 您的sql查询不适合排序日期:您可以使用以下sql查询:
SELECT `id`,`title_name`,`create_time`,`title_img` FROM `news` WHERE `status` = '1' ORDER BY YEAR(`create_time`) DESC, MONTH(`create_time`) DESC, DAY(`create_time`) DESC,`title_img` DESC
之后,您可以使用以下php代码来处理视图:
<?php
//this for is for generate test data...
for($i=0;$i<10;$i++)
{
$row_news[]=array(
'create_time'=>time()+$i*36000,
'title_img'=>"title image $i",
'title_name'=>"title name $i"
);
}
foreach ( $row_news as $value )
{
$value['date'] = date("Y-m-d", $value["create_time"]);
$group[$value['date']][] = $value;
}
foreach($group as $key=>$rows)
{
echo "
<br>======================<Br>
$key<br>
======================<Br><br>";
foreach($rows as $row)
{
if(!empty($row['title_img']))
echo $row['title_img'].'<Br>';
echo $row['title_name'];
echo '<br>';
}
}
?>
如果您看到任何其他信息,请检查您的查询和结果
如果您愿意,可以在问题中添加db查询结果。您的SQL很好。您的PHP需要做一些工作,仅此而已。您需要的所有数据都将在查询中返回。。。您应该处理php代码上的数据,以显示类似的内容…草莓,谢谢,但您能给我任何建议吗?我的意思是,如果该行
title\u img
不为空。首先显示这些…然后,显示标题的另一行\u img为空。这可以在第二个foreach之前使用usort进行更新,以便按照您需要的顺序进行排序。@Sky您在查询中对数组进行了排序。。。。(你是否测试了我的代码并得到了错误的答案?)。非常感谢你的代码。但我的问题是很多数据都没有标题。我需要在“exist title\u img data”之后对这些行进行排序rows@Sky您能为您的问题添加一个示例查询答案吗?
======================
2015-10-20
======================
title image 0
title name 0
======================
2015-10-21
======================
title image 1
title name 1
title image 2
title name 2
======================
2015-10-22
======================
title image 3
title name 3
title image 4
title name 4
======================
2015-10-23
======================
title image 5
title name 5
title image 6
title name 6
title image 7
title name 7
======================
2015-10-24
======================
title image 8
title name 8
title image 9
title name 9