Q:

PHP while(): how to stop after three times? 

I'm trying to make a PHP photo gallery (yes, I've tried other solutions, none work with the setup I want) and I am trying to make this while loop stop after three times, because I want to show three thumbnails per table row.  Here's my current code:
<table>
<tr>
<? php while($row2 = mysql_fetch_assoc($result)) { echo "
<td><a href='URL/".$row2['file']."'><img src='URL/".$row2['file']."'  width='200' height='106' /></a></td>
"; } ? >
</tr>
</table>

I don't remember learning anything about stopping PHP loops, so please help!  It'd be greatly appreciated. 

A:

So what you actually want is 3 images per row.  I think this will work:
<table>
  <? php 
    $i = 0;
    while($row2 = mysql_fetch_assoc($result)) { 
      if($i%3==0)
        echo "<tr>"; 
      echo "<td><a href='URL/".$row2['file']."'><img src='URL/".$row2['file']."'  width='200' height='106' /></a></td>";
      if($i%3==2)
        echo "</tr>";
      $i++;
    }
    if($i%3!=0)
      echo "</tr>";
  ? >
</table>

