Looping Help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Looping Help

Post by AliasBDI »

I have a page that is gathering all of the thumbnails in a directory and displaying them by increments of 6 or 12 (or whatever the user chooses). My loop is working and page numbering is working. But when someone clicks on a page number to view the thumbnails on that page, it seems as though my thumbnail array does not work. Here is my code:

Code: Select all

<?php 
$thumbs = array();  
$directory = opendir($sessionFOLDER);   
while(($file = readdir($directory)) !== false) {
	if($file !== '.' && $file !== '..' && !is_dir($file)) { 
		$thumbs[] = $file;  
	}  
}  
closedir($directory);  
sort($thumbs);

// get records per page number 
if (!$recPerPage) { $recPerPage = 12; }

// paging the records
if (!$numDisplay) { $numDisplay = 1; }
$totalThumbs = sizeof($thumbs);

// divide the amount of $totalThumbs by $recPerPage to determine how many pages needed<br>
$totalPages = ceil($totalThumbs/$recPerPage);
$pageCount = 1;
if (!$pageNum) { $pageNum = 1; }

// set first thumbnail on page
if ($pageNum!=1) { 
	$startThumb = $recPerPage*($pageNum-1);
	$i = $startThumb;
} else { 
	$i = 0;
}
echo $i;
?>
<?php if ($totalPages>1) { // if there is more than one page show this paging table ?>

<table border="0" align="center" cellpadding="1" cellspacing="2">
  <tr>
    <td><h1><strong>[</strong></h1></td>
	<?php while ($pageCount<=$totalPages) { ?>
	<td><h1><a href="browsePhotos.php?<?php echo $_SERVER['QUERY_STRING']; ?>&pageNum=<?php echo $pageCount ?>" class="pagingLinks"><?php echo $pageCount ?></a></h1></td>
	<?php $pageCount = $pageCount+1; ?>
	<?php }  ?>
	<td><h1><strong>]</strong></h1></td>
  </tr>
</table>
<?php } ?>
<hr align="center" width="90%" size="1" id="div" color="#E8E8E8">
<table width="100%" height="100%"  border="0" cellpadding="0" cellspacing="0">
  <?php do { ?>
  <?php $colNUM = 0; ?>
  <tr>
    <?php do { ?>
	<?php $colNUM = $colNUM+1; ?>
	<!-- BEGIN -->
	<td align="center" valign="middle"><table  border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td height="4"><img src="/graphics/home/box_cornertopleft.gif" width="4" height="4"></td>
        <td background="/graphics/home/box_sidetop.gif"><img src="/graphics/home/box_sidetop.gif" width="5" height="4"></td>
        <td height="4"><img src="/graphics/home/box_cornertopright.gif" width="4" height="4"></td>
      </tr>
      <tr>
        <td width="4" background="/graphics/home/box_sideleft.gif"><img src="/graphics/home/box_sideleft.gif" width="4" height="4"></td>
        <td><a href="#" onclick="javascript:window.open('/common/photoDetails.php?filePath=<?php echo str_replace("thumbnails","images",$sessionFOLDER)."/".$thumbs[$i]; ?>','print','width=800,height=700,scrollbars=yes,toolbar=no,status=no,menubar=no,copyhistory=no');"><img src="<?php echo $sessionFOLDER."/".$thumbs[$i]; ?>" border="0"></a></td>
        <td width="4" background="/graphics/home/box_sideright.gif"><img src="/graphics/home/box_sideright.gif" width="4" height="4"></td>
      </tr>
      <tr>
        <td height="4"><img src="/graphics/home/box_cornerbotleft.gif" width="4" height="4"></td>
        <td background="/graphics/home/box_sidebot.gif"><img src="/graphics/home/box_sidebot.gif" width="3" height="4"></td>
        <td height="4"><img src="/graphics/home/box_cornerbotright.gif" width="4" height="4"></td>
      </tr>
    </table>
	</td>
	<!-- END -->
	<?php $i++; ?>
	<?php } while ($colNUM<=5 && $i<$recPerPage); ?>
  </tr>
  <?php } while ($i<$recPerPage); ?>
</table>
<hr align="center" width="90%" size="1" id="div" color="#E8E8E8">
<table border="0" align="center" cellpadding="2" cellspacing="2">
  <tr>
    <td><h1>Paging</h1></td>
    <td><h1>&nbsp;</h1></td>
    <td><h1>&nbsp;</h1></td>
    <td><h1>&nbsp;</h1></td>
    <td><h1>&nbsp;</h1></td>
  </tr>
</table>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

is the query string you are using with the page links correct?
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Post by AliasBDI »

Yes. (I'm pulling files from a directory and not a database). In fact, when I first pull the page everything works. But as soon as I click on a page number (to go to that set of records) it shows only the record that matches the number.

So for instance, with 12 thumbs per page (set by variable $recPerPage) it shows about 19 total pages (which is the $totalThumbs divided by 12, giving me 19 pages with 12 thumbs on each page). But as soon as you click on a page number (any greater than 1) you get only one picture. And it is the first picture of that page's set.

I think that my code is resetting the array to one picture - the picture that matches the first number of the page set. Here is the code that makes this happen:

Code: Select all

// set first thumbnail on page 
if ($pageNum!=1) { 
    $startThumb = $recPerPage*($pageNum-1); 
    $i = $startThumb; 
} else { 
    $i = 0; 
} 
echo $i;
No $pageNum set works and so does $pageNum set to 1. But no other number works. I believe it is because the code (with the $pageNum as 1 or 0 will ultimately become 0 and 0 always works. See, I thought by setting the $i variable to a particular number ...

Code: Select all

$startThumb = $recPerPage*($pageNum-1); $i = $startThumb;
... it would then begin the array loop at that number. But rather, it give me only that number.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the problem is due to usage of $recPerPage later in the file. I'd suggest, instead of setting $i to the starting position, set an $offset variable. Then use $thumbqs[$i + $offset]
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Post by AliasBDI »

I'm sorry, I just don't follow you. Do you mean that I should add an additional variable ($offset)? But how would I implement that into the loop?
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Post by AliasBDI »

Ahh. Figured it out. Thanks for the input.
Post Reply