在foreach循环中分组日期

时间:2015-11-13 17:42:35

标签: php mysql date foreach

我想添加一个新行,以便在foreach循环中对日期进行分组。

示例:

2015-11-05
2015-11-05

2015-11-07

2015-11-10
2015-11-10
2015-11-10

... and so on

由于我不知道如何实现这一点,因为在谷歌搜索几天后我没有找到任何内容,我只有foreach的标准代码来循环数据库中的数据。

SELECT * FROM table ORDER BY datetime_published

foreach($rows AS $row) {
    echo $row['datetime_published'].'<br>';
}

上面的代码打印出这样的日期:

2015-11-05
2015-11-05
2015-11-07
2015-11-10
2015-11-10
2015-11-10
... and so on

如示例所示,我如何实现这一目标?

数据库结构

`id` int(11) NOT NULL AUTO_INCREMENT,
`id_visitor` int(11) NOT NULL,
`id_place` int(11) NOT NULL,
`id_post` int(11) NOT NULL,
`id_category` int(11) NOT NULL,
`data_cover` varchar(50) COLLATE utf32_swedish_ci NOT NULL,
`data_subject` varchar(45) COLLATE utf32_swedish_ci NOT NULL,
`data_content` text COLLATE utf32_swedish_ci NOT NULL,
`datetime_published` datetime NOT NULL,
`datetime_edited` datetime NOT NULL,
`datetime_saved` datetime NOT NULL

3 个答案:

答案 0 :(得分:5)

一种方法是保存以前的值并将其与当前值进行比较:

$prev = NULL;
foreach($rows AS $row) {
    $curr = $row['datetime_published'];
    echo "$curr<br>";
    if ($curr != $prev) {
        echo '<br>';
        $prev = $curr;
    }
}

答案 1 :(得分:1)

$temp="":
foreach($rows AS $row) {
        $my_date = $row['datetime_published'];
        echo $my_date.'<br>';
         if(strcmp($my_date,$temp)==0)
           echo '<br>';
        $temp = $my_date;

    }

答案 2 :(得分:1)

Mureinik和Subin提供的答案都很好。但您也可以选择执行以下操作

  1. 将您的查询编辑为此类
  2. SELECT datetime_published,count(*) as count,`id_visitor`,`id_category`,`datetime_published`,`datetime_edited`,`data_content` FROM table group by `datetime_published` order by `datetime_published` desc
    
    1. 现在你有一个数组,其中包含应该打印这些日期的日期和计数。然后你可以用'for loop'打印它
    2. for ($i = 1; $i <= $row['count']; $i++) {
      echo $row['datetime_published']." ".$row['data_content'] .'<br>';
      }
      echo "<br>";
      

      完整解决方案剧透

      <?php
      
      error_reporting(E_ALL);
      ini_set('display_errors', 1);
      
      $con=mysqli_connect("dbhost","dbuser","dbpass","db");
      // Check connection
      if (mysqli_connect_errno())
        {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }
      $sql = "SELECT datetime_published,count(*) as count FROM date group by `datetime_published` order by `datetime_published` desc";
      //print $sql;
      if ($result=mysqli_query($con,$sql))
        {
        // Fetch one and one row
        while ($row=mysqli_fetch_assoc($result))
          {
      
          $var = "select * from date where datetime_published = '".$row['datetime_published'] ."'";
          print $row['datetime_published']."<br>";
              if ($results=mysqli_query($con,$var))
              {
                  // Fetch one and one row
                  while ($rows=mysqli_fetch_assoc($results))
                  {
                  print $rows['data_subject']. " " . $rows['data_content'];
                  echo "<br>";
                  }
              }
          echo "<br>";
          }
      
      
      }
      echo "<br>";
      
        // Free result set
        mysqli_free_result($result);
      
      
      mysqli_close($con);
      ?>