0

Pretty much I have a code like below that I'm trying to add </tr><tr> to after every 6 results.

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>";
yanike
  • 827
  • 3
  • 13
  • 29

1 Answers1

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:

Whats means if(++$i % 6 == 0) code:

  1. ++$i equals $i = $i + 1;
  2. $i % 6 means $i modulo 6
  3. If $i modulo 6 equals 0 then echo </tr><tr>

So we can write it as:

$i = $i + 1;
if($i % 6 == 0) {
   echo '</tr><tr>';
}

http://php.net/manual/en/internals2.opcodes.mod.php

http://php.net/manual/en/language.operators.increment.php

delphist
  • 4,409
  • 1
  • 22
  • 22
  • Dude!!! Virtual hug and come in for the real thing. Thank You :) I had something somewhat like it, but wasn't getting it right. Thanks. Can you explain the if(++$i % 6 == 0) { part. – yanike Feb 27 '11 at 11:22