我想將結果從數據庫拆分爲每行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();
你可以給一個查詢結果的例子,你想如何格式化?從你的來源來看,你想要做的事情並不是很清楚。 – RonaldBarzell
模數運算符是你的朋友。 – AlienWebguy
請不要將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)。 –