Jump to content

DB Result Counter Loop


LostNights

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)); ?>
Link to comment
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]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.