dongqing7789 2012-12-11 19:04
浏览 47

需要将MySQL结果拆分为每行3个

I'm wanting to split the results from a database into 3 results per row. This is the code I have:

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();
  • 写回答

3条回答 默认 最新

  • dongyulian5801 2012-12-11 19:12
    关注

    As someone pointed out, if you want to do something every Nth time, you generally want a check of the form:

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

    Where $i is your current iteration number, and of course $N is how often you want to break. So in this case, $N = 3, and your breaking logic would go in the body of the if statement.

    Having said that, it looks like there may be more involved than just that; your code already has quite a bit going on in your table, as you already have multiple columns per row. Did you really want 3 sets of those multiple columns, or did you mean something else, like row groupings?

    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改