2011-03-31 58 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"; 
    }