开发者

PHP modulus confusion

开发者 https://www.devze.com 2023-04-12 03:01 出处:网络
I have a table and needs <tr> to be appended every 3 td <?php $sql = mysql_query(\"SELECT * FROM products\");

I have a table and needs <tr> to be appended every 3 td

<?php 
$sql = mysql_query("SELECT * FROM products");
$count = 1;
while($row = mysql_fetch_array($sql)) {
    if($c % 3 == 0) echo "<tr>";
    echo "
    <td>
    $row['name'];
    </td>
   开发者_Go百科 ";
    if($c % 3 == 0) echo "</tr>";
    $c++;
}
?>

This comes up as

[] []
[]
[] []
[]

instead of

[] [] []
[] [] []

where [] means the data placement


You need to switch the </tr> output and the $c++ counting:

    $c++;
    if($c % 3 == 0) echo "</tr>";
}

So that the </tr> is in sync with the next loops <tr>.

0

精彩评论

暂无评论...
验证码 换一张
取 消