将WHILE循环结果分组

时间:2011-03-31 16:16:59

标签: php tags while-loop if-statement

目前我正在使用WHERE循环在我的数据库中显示2列信息。这是代码:

$sql2 = sprintf($rawsql2, mysql_real_escape_string($id));
$result2 = mysql_query($sql2);

/*These if & while statements decide whether or not the information should be displayed. If no results are returned from the query above then an alternative message is shown.*/

if (mysql_num_rows($result2) > 0) {
    while ($row = mysql_fetch_array($result2, MYSQL_ASSOC)) {
        echo $row["open"] . "-" . $row["close"] . "<br/><br/>";
    }
}
else {
    echo "Error";
}

这输出以下内容:

08:00:00-12:30:00

13:30:00-16:30:00

09:00:00-17:00:00

18:00:00-20:00:00

09:00:00-17:00:00

18:00:00-20:00:00

08:00:00-17:00:00

18:00:00-20:00:00

09:00:00-17:00:00

18:00:00-20:00:00

现在,我需要做的是将每2行转换成组,而不是看起来像:

08:00:00-12:30:00 13:30:00-16:30:00

09:00:00-17:00:00 18:00:00-20:00:00

09:00:00-17:00:00 18:00:00-20:00:00

08:00:00-17:00:00 18:00:00-20:00:00

09:00:00-17:00:00 18:00:00-20:00:00

这可能吗?谢谢你的帮助

编辑:感谢大家的所有答案......我使用了Shakti的答案,因为这是我看到的第一个答案,但从外观来看,每个人都非常相似。

8 个答案:

答案 0 :(得分:1)

if(mysql_num_rows($result2) > 0)
{
    $count=0;
    while ($row = mysql_fetch_array($result2, MYSQL_ASSOC))
    {    
        echo $row["open"] . "-" . $row["close"] ;
        if ($count % 2 !=0 )
        {
           echo  "<br/><br/>"; 
        } 
        $count++;    
    }
}

答案 1 :(得分:1)

if(mysql_num_rows($result2) > 0){
    $counter = 0;
    while($row = mysql_fetch_array($result2, MYSQL_ASSOC)){
        echo $row["open"] . "-" . $row["close"] . " ";
        if(++$counter % 2 == 0){
          echo "<br/><br/>";
          $counter = 0;
        } 
    }
}

答案 2 :(得分:1)

试试这个:

$lines = 1;
if(mysql_num_rows($result2) > 0) {
    while ($row = mysql_fetch_array($result2, MYSQL_ASSOC)) {

        echo $row["open"] . "-" . $row["close"];
        echo (($lines%2 == 0)?"<br/><br/>":'');
        $lines++;

    }
} else {
        echo "Error";
}

答案 3 :(得分:0)

  $i=1;
  while ($row = mysql_fetch_array($result2, MYSQL_ASSOC)) {
    if ($i == 1) {
      echo $row["open"] . "-" . $row["close"];
      $i++;
    } else {
      echo $row["open"] . "-" . $row["close"] . "<br /><br />";
      $i=1;
    }

答案 4 :(得分:0)

if($result2) { 
    $printTimes = function($r) {echo $row["open"] . "-" . $row["close"];};
    $newLine = false;
    while ($row = mysql_fetch_assoc($result2)) 
    {
        $printTimes($row);
        echo $newLine ?  '<br /><br />' : ' ';
        $newLine = !$newLine;
    }
}

答案 5 :(得分:0)

我的代码版本。

    $sql2 = sprintf($rawsql2, mysql_real_escape_string($id));
    $result2 = mysql_query($sql2);

    if(mysql_num_rows($result2) > 0) {
        while (true) {
            $row1 = mysql_fetch_array($result2, MYSQL_ASSOC);
            $row2 = mysql_fetch_array($result2, MYSQL_ASSOC);
            if (!($row1 and $row2)) break;
            echo $row1["open"] . "-" . $row1["close"] . " ";
            echo $row2["open"] . "-" . $row2["close"] . "<br/><br/>";
        }
    } else {
        echo "Error";
    }

答案 6 :(得分:-2)

$sql2 = sprintf($rawsql2, mysql_real_escape_string($id));

$result2 = mysql_query($sql2);

/*These if & while statements decide whether or not the information should be displayed. If no results are returned from the query above then an alternative message is shown.*/
int row_count=mysql_num_rows($result2)
if(row_count > 0) {
    while ($row = mysql_fetch_array($result2, MYSQL_ASSOC)) {
        row_count--;
        echo $row["open"] . "-" . $row["close"] ;
        if(row_count > 0 && ($row = mysql_fetch_array($result2, MYSQL_ASSOC)){
          row_count--;   
          echo $row["open"] . "-" . $row["close"] ;
         }
        "<br/><br/>"


    }
} else {
        echo "Error";
}

应该这样做,如果有一个奇数,你可能需要抓住

答案 7 :(得分:-2)

仅在循环的每个其他迭代上输出<br/><br/>。 (顺便说一句,<p><ul><div>会更具语义性。)

$counter = 0;
while ($row = mysql_fetch_array($result2, MYSQL_ASSOC)) {

    echo $row["open"] . "-" . $row["close"] . " ";

    // Break line after every two items
    $counter = ($counter + 1) % 2;
    if ($counter == 0)
        echo "<br/><br/>";
}