2011-03-31 143 views
1

此刻我正在使用WHERE循环来显示数据库中的2列信息。下面的代码:将WHILE循环结果分组

$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 

这可能吗?感谢您的帮助

编辑:感谢所有的答案,每个人......我用沙克蒂的答案,因为这是我看到的第一个,但看起来它的每个人都非常相似。

回答

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++;  
    } 
} 
+0

+1为解决方案。 -1为可怕的,不一致的格式。 – 2011-03-31 16:27:11

+0

@Tom:同意你 – 2011-03-31 16:32:15

+0

然而,你并没有修正它在你的编辑。 – 2011-03-31 16:40:51

-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"; 
} 

应该这样做,U可能需要捕捉,如果有一个奇数

+1

哈哈他是在一个while循环,你需要一些条件 – Neal 2011-03-31 16:21:59

+0

我什至不明白这应该如何工作.... – 2011-03-31 16:28:50

+0

对不起,我忘记了移动指针 – 2011-03-31 16:34:34

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; 
    } 
+2

你有一个语法错误(额外';')和大量的代码重复。 – 2011-03-31 16:27:27

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; 
    } 
} 
+1

你已经复制了MySQL的访问权限。这有点新鲜。 – 2011-03-31 16:28:20

+1

我的代码中没有MySQL访问权限。你指的是我过去如何取得两次?我决定我更喜欢这个,但我没有看到任何错误 – 2011-03-31 16:34:27

+0

@jon_darkstar:正确。您有两次抓取,从MySQL记录集中检索。如何构成“无MySQL访问”是任何人的猜测...... – 2011-03-31 16:37:27

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; 
     } 
    } 
} 
+0

该计数器最终将耗尽。当你走时,最好改变'$ counter'的值。 – 2011-03-31 16:27:57

+0

@Tomalak:你在说什么? '$ counter'在每次迭代中增加。 – 2011-03-31 16:29:18

+0

@火箭:你的'$ counter'变量永远增加。最终,如果记录集中有足够的行,它可能会溢出。这是混乱的编码。相反,对变量本身执行'%2',使它只有'0'或'1'。 – 2011-03-31 16:30:35

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"; 
} 
-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/>"; 
} 
+0

'$ counter'不计算任何东西。这是一个浪费版本的切换。 'num_rows'命令是毫无意义的 - 如果'$ result2'为假而不是资源,并且如果它是一个合适的资源,它的行数为零,那么'while'主体永远不会运行 – 2011-03-31 16:38:17

+1

@jon_darkstar:它会计数模2的行数。它是如何“浪费”?它是一个切换。如果您建议我通过使用整数而不是布尔值来“浪费空间”,那么我的反应是我让PHP担心这样的微观优化。三个字节,即使使用,也不会杀死任何人。 – 2011-03-31 16:38:52

+1

你想挑剔,我也可以挑剔。我知道性能差异并不重要,但名字仍然很烂 – 2011-03-31 16:40:19

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"; 
    }