我想将数据库中的结果拆分为每行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 :(得分:2)
正如有人指出的那样,如果你想每隔N次做一件事,你通常需要检查表格:
if ( $i > 0 && $i % $N == 0) {
// Do your Nth something here
}
其中$ i是您当前的迭代次数,当然$ N是您想要破解的频率。所以在这种情况下,$ N = 3,你的破坏逻辑将进入if语句的主体。
话虽如此,看起来可能涉及的不仅仅是那个;你的代码已经在你的表中进行了很多,因为你已经有了每行多个列。你真的想要3组这么多列,还是你的意思是其他的东西,比如行分组?
答案 1 :(得分:0)
我认为你忘了在if语句中定义$ row,在这种情况下你的变量将只是局部变量而只有
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);
答案 2 :(得分: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;