【问题标题】:Sort by day of the week after filtering, in different rows过滤后按星期几排序,在不同的行中
【发布时间】:2018-02-09 14:35:12
【问题描述】:

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

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

它现在的显示方式:

记录1

记录2

记录3

我希望它显示的方式:

2 月 5 日星期一

记录1

记录2

2 月 6 日星期二

记录3

记录4

等等。我的代码示例,更改了一些内容:

       $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  

【问题讨论】:

  • 我很难看出这与 c# 有什么关系。这看起来很像 PHP。
  • 到目前为止,您尝试了哪些方法来得到答案?
  • 1) 抱歉,已将标签更改为 php。 2)我还没有来回答,是否可以安排结果并在几天内显示?

标签: php sql


【解决方案1】:

如果您的行已经排序,那么实现这一点非常简单。

检查这个例子:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-31
    • 2014-06-26
    • 1970-01-01
    • 1970-01-01
    • 2016-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多