Jump to content

Archived

This topic is now archived and is closed to further replies.

LostNights

DB Result Counter Loop

Recommended Posts

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)); ?>

Share this post


Link to post
Share on other sites
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:
[code]<?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 }
}
?>[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.