开发者

PHP modulus confusion

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜