将SQL输出分组为相同的值

时间:2013-12-08 23:32:02

标签: php mysql sql grouping

我正在尝试创建一个显示商店开口的脚本。 我希望这些开口列在彼此之下。例如,如果周一,周二和周三的开放时间完全相同,我希望将其分组如下:


周一至周三:12:00-15:00

周四:13:00-15:00

周五:10:00-18:00

周六:12:00-18:00

周日:14:00-18:00


在工作日输出工作日时工作正常,但我无法弄清楚,如何以相同的开放时间对工作日进行“分组”。

我尝试使用if($time == $time),但这没效果。我没有任何其他想法如何去做。

我不想从表格中获取工作日名称。这就是我创建$weekdays array以便我可以直接在代码中更改工作日名称的原因。

我还需要将工作日名称和开放时间分开。

这是我的SQL表:

ID |     mon      |     tue      |     wed      |     thu      |     fri      |     sat      |     sun      
1    12:00-15:00    12:00-15:00    12:00-15:00    13:00-15:00    10:00-18:00    12:00-18:00    14:00-18:00

这是我目前的PHP代码:

$get_openings = mysql_query("SELECT * FROM openings WHERE NC_Id='1'");
while($days = mysql_fetch_array($get_openings)) {

   // Loads time only from database
   $mon = $days['mon'];
   $tue = $days['tue'];
   $wed = $days['wed'];
   $thu = $days['thu'];
   $fri = $days['fri'];
   $sat = $days['sat'];
   $sun = $days['sun'];

   $weekdays = array('monday','tuesday','wednesday','thuesday','friday','saturday','sunday');
   $times = array($mon,$tue,$wed,$thu,$fri,$sat,$sun);

   $i = 0;
   foreach($times as $time) {

      while ($i <= 6) {

          if($time == "") {
              echo $weekdays[$i++].": "."Closed<br>";
          }

          else {
              echo $weekdays[$i++].": ".$time."<br>";
          }
       }
    }
}

有人能帮助我吗?

谢谢 - Jesper

2 个答案:

答案 0 :(得分:0)

试试这样:

$previous_time = null;
$previous_day = null;
$consequetive = 0;
while($days = mysql_fetch_assoc($get_openings)) { // loop through your rows
    $days[''] = null;
    foreach ($days as $day => $time) { // loop through your fields
        if ($day != "id") { // skip the `id` field
            if ($previous_time !== $time) {
                echo $consequetive > 0 ? $previous_day : "";
                echo $previous_day ? ": " : "";
                echo $previous_time === "" ? "Closed" : $previous_time;
                echo $previous_day && $day ? "<br>" : "";
                echo $day;
                $consequetive = 0;
            }
            else {
                echo ++$consequetive == 1 ? " - " : "";
            }
            $previous_time = $time;
            $previous_day = $day;
        }
    }
}

答案 1 :(得分:0)

1)在while循环之外定义$weekdays,因为它不会改变

2)定义$mon$tue ...然后在$ times中再次收集它们是没有意义的。您可以直接使用fetch语句中的命名数组。

$weekdays = array('mon','tue','wed','thu','fri','sat','sun');
$get_openings = mysql_query("SELECT * FROM openings WHERE NC_Id='1'");

while($days = mysql_fetch_array($get_openings)) {

  $result = array();
  foreach($weekdays as $day){
    $time = $days[$day]==''?'closed':$days[$day];
    if(count($result)>0 and $result[0][1]==$time){
      $result[0][0] = substr($result[0][0],0,3) . '-' . $day;
    } else {
      array_unshift($result,array($day,$time));
    }
  }
  $result = array_reverse($result);
  /* add code to display $result here */
}

在每个循环中$result是一个包含日期名称和开放时间的数组数组。例如:array(array('mon-wed','12:00-15:00'),array('thu',...),...)     * /