简体   繁体   English

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

[英]Need to split MySQL results into 3 per row

I'm wanting to split the results from a database into 3 results per row. 我想将数据库中的结果拆分为每行3个结果。 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();

As someone pointed out, if you want to do something every Nth time, you generally want a check of the form: 正如有人指出的那样,如果你想每隔N次做一些事情,你通常需要检查一下表格:

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. 其中$ i是您当前的迭代次数,当然$ N是您想要破解的频率。 So in this case, $N = 3, and your breaking logic would go in the body of the if statement. 所以在这种情况下,$ N = 3,你的破坏逻辑将进入if语句的主体。

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? 你真的想要3组这么多列,还是你的意思是其他的东西,比如行分组?

I think you forget to define the $row over the if statement , in this case your variable will be local variable inside the while only 我想你忘了在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);

I think this may help you with what you are trying to do. 我认为这可以帮助你做你想做的事情。 Of course you will need to style the table to meet your needs. 当然,您需要设置表格样式以满足您的需求。 You can set the number of columns in the code to whatever fits for you. 您可以将代码中的列数设置为适合您的列数。

$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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM