PHP只显示$ value,直到$ value更改

时间:2015-04-29 07:37:29

标签: php

您好我有从我的数据库生成的下表,它看起来像这样

enter image description here

问题是我不想重复日期。如果特定日期有多个灯具,我只希望日期显示一次,然后是日期下的灯具。

示例在这种情况下,在图像上,今天,2015-04-23 只能显示一次,然后只显示2个团队名称。

我正在使用以下代码

$echoed = false;

while($row = > $played){

$gameDate = $row['event_date'];
$team1 = $row['team1'];
$team2 = $row['team2'];
$venue = $row['venue'];
$eventId = $row['event_id'];



    if($gameDate == $date && !$echoed){

    echo'<tr>'; 
    echo $echoed ='<td>Today,'.$gameDate.'echo</td>';
    echo'</tr>';
    echo'<tr>';
        echo'<td>'.$team1.'</td>';
        echo'<td>'.$team2.'</td>';
        echo'<td>'.$venue.'</td>';
        :
        :

但是上面的代码没有给出所需的结果,并返回与上面的图像相同的结果

3 个答案:

答案 0 :(得分:1)

您可以使用$echoed变量来跟踪您要打印的当前日期,而不是像$date那样使用// I am using $date as it seems undefined, but if you already use that, name it something else $date = false; while($row = > $played){ $gameDate = $row['event_date']; $team1 = $row['team1']; $team2 = $row['team2']; $venue = $row['venue']; $eventId = $row['event_id']; if($gameDate !== $date){ $date = $gameDate; echo'<tr>'; echo $echoed ='<td>Today,'.$gameDate.'echo</td>'; echo'</tr>'; } echo'<tr>'; echo'<td>'.$team1.'</td>'; echo'<td>'.$team2.'</td>'; echo'<td>'.$venue.'</td>'; ... 。然后,只有在更改时才会打印日期。

类似的东西:

public class List {
    private Node head = null;

    // [...] Other methods

    public Node findNthLastRecursive(int nth) {
        if (nth <= 0) return null;
        return this.findNthLastRecursive(this.head, nth, new int[] {0});
    }

    private Node findNthLastRecursive(Node p, int nth, int[] pos) {
        if (p == null) {
            return null;
        }
        Node n = findNthLastRecursive(p.next, nth, pos);
        pos[0]++;
        if (pos[0] == nth) {
            n = p;
        }
        return n;
    }
}

答案 1 :(得分:1)

如果日期与之前的日期相同,您可以查看:

<div id="toolbar">

注意:我删除了您的回显代码和if语句,因为$ date未定义:)

答案 2 :(得分:0)

保存每个循环中的最后一个日期,并在当前步骤中检查日期。

$last_date = '';

while (...) {
    $gameDate = $row['event_date'];
    // set other variables

    if ($gameDate != $last_date) {
        // write $gameDate here
    }

    // teams table here    

    $last_date = $gameDate;
}