Jump to content

php counter


loz

Recommended Posts

In a galley I am working on there is a counter that sets the number of
images wanted in the row and
when that number is exceeded creates a new row of thumbnails:
http://www.justict.co.uk/galleries.php
<ul>
<?php
$counter = 0;
do { ?>

      <li> thumbnail images here</li>

<?php
  $counter++;
  if($counter % 4 == 0)
  {
    echo "</ul><ul>";

That works fine unless the number of images is 4, 8, 12, etc and then there
is an extra blank row shown.
any help appreciated
loz
Link to comment
Share on other sites

just rework the way you check for the end of your row:
[code]
<?php
$counter = 0;
while ($row = mysql_fetch_array($sql)) {
  if ($counter % 4 == 0) echo "<ul>\n"; // check for start of row
  // echo your item here
  $counter++;
  if ($counter % 4 == 0) echo "</ul>\n";
}
// now that we're done, check to see if the last row was closed
if ($counter %4 != 0) echo "</ul>\n";
?>
[/code]

hope this helps
Link to comment
Share on other sites

Hi,
Thanks for that, I cannot get the sytax right.
The repeating ul code looks like this:
[size=8pt]  [color=green]<ul>
    <?php
$counter = 0;
do { ?>
      <li><div><?php echo $row_rsGetImages['img_description']; ?></div><a href="galleries/<?php echo $row_rsGetImages['gallery_folder']; ?>/fullsize/<?php echo getFullName($row_rsGetImages['img_filename']); ?>"><img src="galleries/<?php echo $row_rsGetImages['gallery_folder']; ?>/thumbs/<?php echo $imgThumb = getThumbName($row_rsGetImages['img_filename']); ?>" alt="" width="60" height="60" title="<?php echo $row_rsGetImages['img_caption']; ?>" <?php echo picSize("galleries/".$row_rsGetImages['gallery_folder']."/thumbs/".$imgThumb); ?>></a>      </li>
<?php
$counter++; 
if($counter % 4 == 0)
{
echo "</ul><ul>";
}
  } while ($row_rsGetImages = mysql_fetch_assoc($rsGetImages)); ?>
  </ul>[/color][/size]

Any more help really appreciated :)

cheers

loz
Link to comment
Share on other sites

try this:
[code]
<?php
$counter = 0;
while ($row_rsGetImages = mysql_fetch_assoc($rsGetImages)) {
  if ($counter % 4 == 0) echo "<ul>\n";
?>
<li><div><?php echo $row_rsGetImages['img_description']; ?></div><a href="galleries/<?php echo $row_rsGetImages['gallery_folder']; ?>/fullsize/<?php echo getFullName($row_rsGetImages['img_filename']); ?>"><img src="galleries/<?php echo $row_rsGetImages['gallery_folder']; ?>/thumbs/<?php echo $imgThumb = getThumbName($row_rsGetImages['img_filename']); ?>" alt="" width="60" height="60" title="<?php echo $row_rsGetImages['img_caption']; ?>" <?php echo picSize("galleries/".$row_rsGetImages['gallery_folder']."/thumbs/".$imgThumb); ?>>[/url]      </li>
<?php
  $counter++;
  if($counter % 4 == 0) echo "</ul>\n";
}
if ($counter % 4 != 0) echo "</ul>\n";
?>
[/code]

the difference: [b]DO NOT[/b] prime the loop with [tt]$row_rsGetImages = mysql_fetch_assoc($rsGetImages)[/tt] before you start the while or you'll end up missing your first image every time.
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.