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

Php 需要将MySQL结果拆分为每行3个,php,mysql,limit,rows,Php,Mysql,Limit,Rows,我想把数据库中的结果分成每行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($resu

我想把数据库中的结果分成每行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();
include(“connect.php”);
$result=mysql_查询(“按标题从电影顺序中选择*);
$num=mysql\u num\u行($result);
如果($num>0){
$i=0;
而($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.=''.$title'.
.$rating'.
运行时:'.$length'.'; ++$i; } } 否则{ $row='未找到'; } mysql_close();
正如有人指出的,如果你想每N次做一件事,你通常需要检查一下表格:

if ( $i > 0 && $i % $N == 0) {
    // Do your Nth something here
}
其中,$i是您当前的迭代次数,$N当然是您希望中断的频率。在本例中,$N=3,中断逻辑将出现在if语句的主体中


话虽如此,但看起来可能不止如此;您的代码已经在您的表中进行了相当多的操作,因为每行已经有多个列。您是真的想要3组这些多列,还是指其他的,比如行分组?

我想您忘了在if语句上定义$row,在这种情况下,您的变量将是while-only语句中的局部变量

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);
include(“connect.php”);
$result=mysql_查询(“按标题从电影顺序中选择*);
$num=mysql\u num\u行($result);
$row='';
如果($num>0){
$i=0;
而($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.='
“.$title.”
“.$rating.”
运行时:“.$length.” '; ++$i;} }else{$row='Nothing found';} mysql_close(); 打印(行);
我认为这可能有助于您尝试做的事情。当然,您需要根据自己的需要设计桌子的样式。您可以将代码中的列数设置为适合您的任何列数

$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;
$mysqli=新的mysqli(DB\u主机、DB\u用户名、DB\u密码、DB\u名称);
如果($mysqli->connect\u errno)
{
echo“未能连接到MySQL:(“$mysqli->connect\u errno.”“$mysqli->connect\u error;
$mysqli->close();
}
$values=$mysqli->query(“按标题从电影订单中选择*);
$cols=3;
$i=1;
回音“.PHP_EOL;
回音“.PHP_EOL;
而($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))
{
回音“.PHP_EOL;
//你想包含的内容都在这里
回音“.PHP_EOL;
回音“.PHP_EOL;
回音“.PHP_EOL;
}
其他的
{
回音“.PHP_EOL;
//你想包含的内容都在这里
回音“.PHP_EOL;
}
$i++;
}
回音“.PHP_EOL;
回音“.PHP_EOL;

您能否给出一个查询结果的示例以及您希望如何格式化?从你的源代码中不太清楚你想做什么。模运算符是你的朋友。请不要在新代码中使用mysql_*函数。它们不再得到维护,社区已开始恢复。看到了吗?相反,你应该学习并使用或。如果你不能决定,将帮助你做出选择。如果你想学的话,这里有一个。@johncode他们现在已经正式被弃用了。更新后的数据库不使用ext-mysqlcomment@Nick愤怒,谢谢你的更新!