循环中将所有项添加到特定条件

时间:2015-04-23 17:52:56

标签: php

当$ date符合条件时,我希望今天在标题下显示$ date的所有游戏,但是此时我的代码正在循环并为每个条件创建一个新标题,其中$ date = today,如图所示。 今天应该只有一个标题,然后跳到第二天等

enter image description here

while($row>$played){
            if($row['event_date']== $date){
            echo'<td>TODAY,'.$date.'</td>';
            :
            :
             }

3 个答案:

答案 0 :(得分:3)

echo一次并记录下事实:

$echoed = false;

while($row>$played){   // not sure what this is doing $row is an array
    if($row['event_date'] == $date && !$echoed){
        echo $echoed = '<td>TODAY,'.$date.'</td>';
    }
}

答案 1 :(得分:1)

尝试保留跟踪上次日期的变量,然后当最后日期与记录中的日期不同时,打印新标题...

$lastdate = "";
while($row>$played){
  if($row['event_date']== $date){
    if ($lastdate != $date) {
      echo'<td>TODAY,'.$date.'</td>';
      $lastdate = $date;
    } 
    : #<- display match details here
    :
  }
}

答案 2 :(得分:0)

假设数据没有排序或排序,建议将结果存储在php associative array中。 key =&gt;阵列

示例:

$games = array();
while ($row > $played) {

    // for readability, named as date
    $date = $row['event_date'];

    // initialise as a new entry, if it hasnt already
    if (!isset($games[$date]) {
      $games[$date] = array();
    }

    // append data to array 
    $games[$date][] = ... // data added here
}

这将产生一个数据结构,如;

Array 
   2015-04-01 => [ delhi, crusaders ],
   2015-04-04 => [ a, c, d, b ],
   .
   . 

您可以使用foreach轻松输出或格式化。 希望这会有所帮助。