按时间和顺序排序,不为空

时间:2015-10-20 14:11:45

标签: 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->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无法做到。

我需要按日期分组来分区内容。

  1. 首先DESC create_time

  2. 如果title_img不为null,则显示内容行....

  3. 然后显示title_img的行为空

  4. 但是,现在查询anwer:

    ======================
    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演示:http://jsfiddle.net/m7c33nzL/1/

    有什么建议吗?

1 个答案:

答案 0 :(得分:1)

你真正的问题是你的查询...... 你的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>';
    }
}
?>

答案是这样的:

======================
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

如果你看到其他任何内容,请检查你的查询和结果......

如果你愿意,可以在你的问题中添加db query的结果。