Jump to content


Photo

php counter


  • Please log in to reply
4 replies to this topic

#1 loz

loz
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 03 October 2006 - 12:28 PM

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

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 03 October 2006 - 01:02 PM

just rework the way you check for the end of your row:
<?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";
?>

hope this helps
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 loz

loz
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 03 October 2006 - 01:28 PM

Hi,
Thanks for that, I cannot get the sytax right.
The repeating ul code looks like this:
  <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>


Any more help really appreciated :)

cheers

loz

#4 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 03 October 2006 - 01:35 PM

try this:
<?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";
?>

the difference: DO NOT prime the loop with $row_rsGetImages = mysql_fetch_assoc($rsGetImages) before you start the while or you'll end up missing your first image every time.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#5 loz

loz
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 03 October 2006 - 01:51 PM

Hi,
The gallery throws a javascript error and the empty row remains.
Appreciate the time you have spent on this but will have to try something different
cheers
loz




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users