我为私人目的创建电影数据库。作为数据库的一部分,我希望能够通过按类型排序,开始播放电影或只显示所有电影来显示电影。
我想在三列表中显示匹配,直到打印出数据库表中的所有行。
例如:
Ice age 1 Ice age 2 Ice age 3
Die Hard 1 Die Hard 2 Die hard 3
下面的代码会发生什么(在显示所有电影时使用)是在39行后停止,即使我知道50行,mysql_num_rows()返回50,当我刚刚打印出来时在1列中完成db表我得到了50行。
$counterone = 0;
$countertwo = 0;
if ($_movietype == 'showmeeverything')
{
$movieresult = mysql_query("SELECT url,title FROM movies ORDER BY title");
if(mysql_num_rows($movieresult) == 0)
{
nomovie();
}
echo '<p align="center"><a href="index.php">Go back</a></p>';
echo '<table border="1" cellspacing="2" cellpadding=2" align="center">';
echo '<tr><td colspan="3" align="center"><b>Title</b></td></tr>';
echo mysql_num_rows($movieresult);
while ($counterone < mysql_num_rows($movieresult))
{
$counterone++;
echo '<tr>';
while (($result = mysql_fetch_array($movieresult)) && $countertwo < 3)
{
echo '<td>';
echo '<a href="'.$result['url'].'">' . $result['title'] . '</a>';
echo '</td>';
$countertwo++;
}
echo '</tr>';
$countertwo = 0;
}
echo '</table>';
echo '<p align="center"><a align="center" href="index.php">Go back</a></p>';
}
答案 0 :(得分:1)
您没有在正确的位置递增$counterone
。你正在计算表ROWS,但正在处理多个记录。 $ counterone应该在内部while($result)
循环内。一旦它存在,$ countertwo是多余的。
请改为尝试:
$counter = 0;
while($row = mysql_fetch_array($movieresult)) {
if ($counter % 3 == 0)
echo '<tr>';
}
echo "<td> blah blah blah </td>";
if ($counter % 3 == 2) {
echo '</tr>';
}
$counter++;
}
答案 1 :(得分:0)
试试这个:
$counterone = 0;
$countertwo = 0;
if ($_movietype == 'showmeeverything')
{
$movieresult = mysql_query("SELECT url,title FROM movies ORDER BY title");
if(mysql_num_rows($movieresult) == 0)
{
nomovie();
}
echo '<p align="center"><a href="index.php">Go back</a></p>';
echo '<table border="1" cellspacing="2" cellpadding=2" align="center">';
echo '<tr><td colspan="3" align="center"><b>Title</b></td></tr>';
echo mysql_num_rows($movieresult);
$numrows = mysql_num_rows($movieresult);
while ($counterone < $numrows)
{
$counterone++;
echo '<tr>';
while (($result = mysql_fetch_array($movieresult)) && $countertwo < 3)
{
echo '<td>';
echo '<a href="'.$result['url'].'">' . $result['title'] . '</a>';
echo '</td>';
$countertwo++;
}
echo '</tr>';
$countertwo = 0;
}
echo '</table>';
echo '<p align="center"><a align="center" href="index.php">Go back</a></p>';
}