2012-12-11 45 views
3

我想將結果從數據庫拆分爲每行3個結果。這是我的代碼:需要將MySQL結果拆分爲每行3個

include("connect.php"); 

$result = mysql_query("SELECT * FROM movies ORDER BY title"); 
$num = mysql_num_rows ($result); 

if ($num > 0) { 
    $i=0; 
    while ($i < $num) { 
     $id = stripslashes(mysql_result($result,$i,"id")); 
     $title = stripslashes(mysql_result($result,$i,"title")); 
     $description = stripslashes(mysql_result($result,$i,"description")); 
     $length = stripslashes(mysql_result($result,$i,"length")); 
     $link = stripslashes(mysql_result($result,$i,"link")); 
     $rating = stripslashes(mysql_result($result,$i,"rating")); 
     $cover = stripslashes(mysql_result($result,$i,"icover")); 

     $row .= '<tr><td><a href="view.php?id='.$id.'"><img width=130 height=190 src="images/covers/'.$cover.'" /></a></td><td valign="top">'.$title.'<br />'.$rating.'<br />Run Time: '.$length.'</td><td><a href="update.php?id='.$id.'">Update</a></td><td><a href="delete.php?id='.$id.'">Delete</a></td></tr>'; 

     ++$i; 
    } 
} 
else { 
    $row = '<tr><td colspan="2" align="center">Nothing found</td></tr>'; 
} 

mysql_close(); 
+0

你可以給一個查詢結果的例子,你想如何格式化?從你的來源來看,你想要做的事情並不是很清楚。 – RonaldBarzell

+5

模數運算符是你的朋友。 – AlienWebguy

+4

請不要將mysql_ *函數用於新代碼。他們不再被維護,社區已經開始[棄用流程](http://goo.gl/KJveJ)。看到[紅色框](http://goo.gl/GPmFd)?相反,您應該瞭解[準備好的語句](http://goo.gl/vn8zQ)並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli的)。如果你不能決定,[本文](http://goo.gl/3gqF9)將有助於選擇。如果你在意學習,這裏是[很好的PDO教程](http://goo.gl/vFWnC)。 –

回答

2

正如有人指出的那樣,如果你想要做的事每個第N次,你通常需要形式的檢查:

if ($i > 0 && $i % $N == 0) { 
    // Do your Nth something here 
} 

其中$ i是你的當前迭代次數,當然$ N是你想要中斷的頻率。所以在這種情況下,$ N = 3,你的破壞邏輯將在if語句的主體中出現。

話雖如此,看起來可能有更多的參與,你的代碼在你的表中已經有很多了,因爲你已經有了每行多個列。你真的想要3組這樣的多列,還是你的意思是別的,比如行分組?

0

我想你忘記了if語句來定義$行,在這種情況下,您的變量將是同時內局部變量只

include("connect.php"); 

$result = mysql_query("SELECT * FROM movies ORDER BY title"); 
$num = mysql_num_rows ($result); 
$row = ''; 

if ($num > 0) { 
    $i=0; 


    while ($i < $num) { 

    $id = stripslashes(mysql_result($result,$i,"id")); 
    $title = stripslashes(mysql_result($result,$i,"title")); 
    $description = stripslashes(mysql_result($result,$i,"description")); 
    $length = stripslashes(mysql_result($result,$i,"length")); 
    $link = stripslashes(mysql_result($result,$i,"link")); 
    $rating = stripslashes(mysql_result($result,$i,"rating")); 
    $cover = stripslashes(mysql_result($result,$i,"icover")); 

    $row .= '<tr> 
    <td><a href="view.php?id='.$id.'"><img width=130 height=190 src="images/covers/'.$cover.'" /></a></td> 
    <td valign="top">'.$title.'<br />'.$rating.'<br />Run Time: '.$length.'</td> 
    <td><a href="update.php?id='.$id.'">Update</a></td> 
    <td><a href="delete.php?id='.$id.'">Delete</a></td> 
    </tr>'; 

    ++$i; } 
} else { $row = '<tr><td colspan="2" align="center">Nothing found</td></tr>'; } 

    mysql_close(); 

    print($row); 
0

我認爲這可以幫助你與你所想做。當然,您需要設計桌子來滿足您的需求。您可以將代碼中的列數設置爲適合您的任何值。

$mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME); 
     if ($mysqli->connect_errno) 
     { 
      echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; 
      $mysqli->close(); 
     } 

    $values = $mysqli->query("SELECT * FROM movies ORDER BY title"); 
    $cols = 3; 
    $i =1; 
    echo "<table>".PHP_EOL; 
    echo "<tr>".PHP_EOL; 
while($row = $values->fetch_assoc()) 
    { 
     $id    = $row['id']; 
     $title   = $row['title']; 
     $description = $row['description']; 
     $length   = $row['length']; 
     $link   = $row['link']; 
     $rating   = $row['rating']; 
     $cover   = $row['icover']; 
    if (is_int($i/$cols)) 
     { 
      echo "<td >".PHP_EOL; 
      //what ever you want to include goes here 
      echo "</td>".PHP_EOL; 
      echo "</tr>".PHP_EOL; 
      echo "<tr>".PHP_EOL; 
     } 
     else 
      { 
       echo "<td >".PHP_EOL; 
       //what ever you want to include goes here 
       echo "</td>".PHP_EOL; 
      } 
      $i++; 
    } 
     echo "</tr>".PHP_EOL; 
     echo "</table>".PHP_EOL;