0

我有一个循环匹配回合:

while ( $matches->fetch() ) 

{

echo $round . '.'; //need a condition to only echo round if it is for the first time
echo $team1 . ' VS ' . $team2;

}

所以,而不是结果,例如:

1.FC BARCELONA VS REAL MADRID
1.MALAGA VS SEVILLA
1.ATLETICO BILBAO VS MADRID
1.MALLORCA VS REAL MADRID
2.FC BARCELONA VS REAL MADRID
2.MALAGA VS SEVILLA
2.ATLETICO BILBAO VS MADRID
2.MALLORCA VS REAL MADRID

我只需要在第一行中回显一次回合数,如下所示:

1.FC BARCELONA VS REAL MADRID
MALAGA VS SEVILLA
ATLETICO BILBAO VS MADRID
MALLORCA VS REAL MADRID
2.FC BARCELONA VS REAL MADRID
MALAGA VS SEVILLA
ATLETICO BILBAO VS MADRID
MALLORCA VS REAL MADRID

如何在while循环中做到这一点?我需要一些 if 或其他条件,但我不知道如何对此进行编码,因此不胜感激。

我已经正确订购了它们,但现在需要做输出部分。

4

5 回答 5

2

跟踪最后一轮,仅在更改时显示。

$lastRound = null;
while ( $matches->fetch() ) 
{
    if ($lastRound != $round) 
    {
        echo $round . '.'; //need a condition to only echo round if it is for the first time
    }
    $lastRound = $round;
    echo $team1 . ' VS ' . $team2;
}
于 2012-10-30T16:12:14.573 回答
1

试试这个:

$written = false;
while ( $matches->fetch() ) {
  if(!$written) {
    echo $round . '.'; //need a condition to only echo round if it is for the first time
    $written = true;
  }
  echo $team1 . ' VS ' . $team2;
}
于 2012-10-30T16:11:43.383 回答
0

之前 while 定义 $count = 0; 在里面,而第一件事就是做 $count++; 然后检查

if ($count == 1) { display round number }
于 2012-10-30T16:10:25.167 回答
0
$i = 0;
while($row = $sql->fetch_assoc()) {
    if($i == 0)
        echo '1. ';
    echo $row['field'];

    $i++;
}
于 2012-10-30T16:11:35.580 回答
0

所有你需要的是

$last = null;
while ( $matches->fetch() ) 
{
    ($round !== $last) and print($round . '.');
    echo $team1 . ' VS ' . $team2;
    $last = $round;
}
于 2012-10-30T16:13:04.917 回答