3

我想将数据库中的结果分成每行 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();
4

3 回答 3

2

正如有人指出的那样,如果您想每 N 次执行一次,您通常需要检查表单:

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

其中 $i 是您当前的迭代次数,当然 $N 是您想要中断的频率。所以在这种情况下,$N = 3,你的中断逻辑将进入 if 语句的主体。

话虽如此,看起来可能涉及的不仅仅是这些;您的代码已经在您的表中进行了很多操作,因为您每行已经有多个列。你真的想要 3 组这些多列,还是你的意思是别的,比如行分组?

于 2012-12-11T19:12:03.920 回答
0

我认为您忘记在 if 语句上定义 $row ,在这种情况下,您的变量将是 while 内的局部变量

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);
于 2012-12-11T19:18:55.700 回答
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;
于 2012-12-11T19:51:10.773 回答