Splitting A Loop?

Hey all,

Been a while since I’ve had to do some real PHP and I’ve come across an issue,

I am doing a simple mysql result fetch and then the values are being shown using a while loop like so:


$query = 'SELECT name, place FROM table ORDER BY id DESC';
$sql = mysql_query($sql);

while($row = mysql_fetch_array($query)
{
/* this is what the loop is currently showing:

<div class="row">
1) john, usa
2) candy, canada
3) sherlock, canada
4) james, australia
</div>

*/
}

What I am trying to do is, if there are more than (for example) 2 results … I need the DIV (class=row) to end, and create a new div.

So for example if there were 4 results, it’d look like this instead of the example above:

<div class="row">
1) john, usa
2) candy, canada
</div>

<div class="row">
3) sherlock, canada
4) james, australia
</div>

Hope this makes sense, if there is a term for this behavior I’d love to know… but yes, if anyone can point me towards the right direction it’d be great.


$query = 'SELECT name, place FROM table ORDER BY id DESC';
$sql = mysql_query($sql);

$rowcounter = 0;
while ($row = mysql_fetch_array($query)) {
  $rowcounter++;
  if ($rowcounter == 1) echo '<div class="row">';
  echo $row['name'] . ', ' . $row['place'] . '<br/>';
  if ($rowcounter == 4) {
    echo '</div>';
    $rowcounter = 0;
  }
} 
if ($rowcounter > 0) echo '</div>';