按过滤后的星期几,按不同的行排序

时间:2018-02-09 14:35:13

标签: php sql

我的应用程序当前正在做什么:最初,它显示数据库的记录。我在两个日期之间选择,我按过滤器。它只显示记录。

我想做什么:我想按天分类结果。要显示,周一5月2日,这些注册,星期二6,这些等,一周中的每一天。我希望它们在结果中按行和列分类。

现在显示的方式:

Record1

record2

RECORD3

我希望它显示的方式:

周一5月5日

RECORD1

RECORD2

周二6月2日

RECORD3

record4

等。我的代码示例改变了一些事情:

       $connect = mysqli_connect(1,2,3,4)
         $query =  "select startdate, registerdate, name FROM `table1` order by
registerdate desc";  
            $result = mysqli_query($connect,  $query);  
     <html>  
          <head>  
               <title></title>  
                    <div id="tablediv">  
                         <table class="table table-bordered" id="testTable">  
                              <tr>  
                                   <th width="30%">start</th>  
                                   <th width="30%">registered</th>  
                                   <th width="30%">name</th>  
                              </tr>   
                         <?php  
                         while($row = mysqli_fetch_array($result))  
                         {  
                         ?>  
                              <tr>  
                                   <td><?php echo $row["startdate"]; ?></td>  
                                   <td><?php echo $row["registerdate"]; ?></td>                                
                                   <td> <?php echo $row["name"]; ?></td>  
                              </tr>    

过滤文件:

   $query =  "start, registerdate, name FROM `table1`"  WHERE registerdate BETWEEN '".$_POST["from_date"]."' AND '".$_POST["to_date"]. "' order by registerdate desc  

1 个答案:

答案 0 :(得分:0)

如果您的行已经订购,那么实现此功能非常简单。

检查此示例:

<?php

$dates = [
    '2018-01-01',
    '2018-01-01',
    '2018-01-02',
    '2018-01-03',
    '2018-01-04',
    '2018-01-04',
    '2018-01-04',
    '2018-01-05',
    '2018-01-05',
    '2018-01-06',    
];

$lastDate = '';
foreach ($dates as $date) {
    $datetime = new DateTime($date);
    if ($date != $lastDate) {
        $lastDate = $date;
        echo "\n" . $datetime->format('j M Y') . "\n";
    }
    echo $datetime->format('Y-m-d') . " - your row here.\n";
}

将输出:

1 Jan 2018 
2018-01-01 - your row here. 
2018-01-01 - your row here. 

2 Jan 2018 
2018-01-02 - your row here. 

3 Jan 2018 
2018-01-03 - your row here. 

4 Jan 2018 
2018-01-04 - your row here. 
2018-01-04 - your row here. 
2018-01-04 - your row here. 

5 Jan 2018 
2018-01-05 - your row here. 
2018-01-05 - your row here. 

6 Jan 2018 
2018-01-06 - your row here.

在这里玩游戏:https://3v4l.org/f7TDr