Jump to content


Photo

DB Result Counter Loop


  • Please log in to reply
1 reply to this topic

#1 LostNights

LostNights
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 05 May 2006 - 02:03 PM

I want to echo out a page <span class="breaker"></span> after every 12 database results.

<?php do { ?>
<div class="employee">
<p>
<img src="images/<?php echo $row_Recordset1['firstname']; ?>_<?php echo $row_Recordset1['lastname']; ?>.jpg" width="140" height="140" /><br />
<?php echo $row_Recordset1['firstname']; ?><br />
<?php echo $row_Recordset1['lastname']; ?><br />
<?php echo $row_Recordset1['office']; ?><br />
<?php echo $row_Recordset1['phone']; ?>
</p>
</div>

<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>

#2 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 05 May 2006 - 02:22 PM

All you have to do is add a counter variable to the loop. I'm going to modify your code, because do-while loops aren't the normal or preferred way of looping through a recordset... because it will ALWAYS do one before checking the condition and I'm not sure how you're getting your first record that way either... and for that matter, you're jumping in and out of PHP tags FAR too often:
<?php 
if($Recordset1 && mysql_num_rows($Recordset1) > 0)
{
$counter = 0;
while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));  
{ 
     if(++$counter == 12)
     {
          echo '<span class="breaker"></span>'; 
          $counter = 0;
     }
     extract($row_Recordset1);
?>
<div class="employee">
<p>
<img src="images/<?=$firstname?>_<?=$lastname?>.jpg" width="140" height="140" /><br />
<?php

echo "$firstname<br />
         $lastname<br />
         $office<br />
         $phone</p>"; ?>
</div>

<?php }
}
 ?>


Info: PHP Manual





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users