我差不多有下面的代码,我试图在每6个结果后添加</tr><tr>
。
echo "<table><tr>";
$query="SELECT * WHERE id='$id' ORDER BY date ASC";
$result=mysql_query($query);
if (mysql_num_rows($result) > 0) {
while($rts = mysql_fetch_array($result)){
$cdata1 = $rts['cdata1'];
$cdata2 = $rts['cdata2'];
echo "<td>$cdata1 and $cdata2</td>";
}
}else{
echo "<td>no results</td>";
}
echo "</tr></table>";
答案 0 :(得分:1)
echo "<table><tr>";
$query="SELECT * WHERE id='$id' ORDER BY date ASC";
$result=mysql_query($query);
$i = 0;
if (mysql_num_rows($result) > 0) {
while($rts = mysql_fetch_array($result)){
$cdata1 = $rts['cdata1'];
$cdata2 = $rts['cdata2'];
echo "<td>$cdata1 and $cdata2</td>";
if(++$i % 6 == 0) {
echo '</tr><tr>';
}
}
}else{
echo "<td>no results</td>";
}
echo "</tr></table>";
<强> UPD:强>
什么意思是if(++$i % 6 == 0)
代码:
++$i
等于$i = $i + 1;
$i % 6
表示$i modulo 6
</tr><tr>
所以我们可以把它写成:
$i = $i + 1;
if($i % 6 == 0) {
echo '</tr><tr>';
}