Hi,

I had a topic a while back and the original issue was solved, however now I've gotten stuck a bit further down the road. Essentially, I take all the images from a folder and make a gallery out of them. I turned to the webmonkey tutorial to limit the number of images per page and everything works great, except I'm having trouble starting the array pointer at the appropriate point for a given page.

The only idea I have is to use "array_shift" and iterate it until the page number is reached... but I'm not quite sure how to do that.

Here's my code:

<?php
if(!isSet($_REQUEST["start"])){
      $_REQUEST["start"] = 0;
}
$i = $_REQUEST["start"];
$photos_per_page = 12;
$prev_start = $_REQUEST["start"] - $photos_per_page;
$next_start = $_REQUEST["start"] + $photos_per_page;


$exts = array("png","gif","jpg");
$path = "/Users/harrison/Sites/images/photos"; 
$handle = opendir($path); 
$files = array(); 
$total_photos = 48;

while($file = readdir($handle)) { 
   if(in_array(substr(strtolower($file),-3),$exts)) { 
      $files[$file] = filemtime($path."/".$file); 
   } 
} 
closedir($handle); 
arsort($files); 



foreach($files as $key => $value) {


if ($i >= $_REQUEST["start"] && $i < $next_start && $i <= $total_photos && $i >= 0) {
	echo "<div class='gallery'>\n<a href='images/photos/display.shtml/".$key."' class='newWindow'>\n<img alt='".$key."' class='gallery' src='images/photos/thumbnails/".$key."' />\n</a>\n</div>\n\n"; 
	$i++;
	} 
}




?>






<?php
//print out navigation links
if(($_REQUEST["start"] == 0) && ($next_start < $total_photos)){

//you're at the beginning of the photo gallery

?>

  <a href="archives/pixels/photographs/index.php?start=<?php print($next_start); ?>">next page</a>&#187;

<?php
} 
elseif (($_REQUEST["start"] > 0) && ($next_start < $total_photos)){ 

//you're in the middle of the photo gallery

?>

  &#171;<a href="archives/pixels/photographs/index.php?start=<?php print($prev_start); ?>">prev page</a>
  |
  <a href="archives/pixels/photographs/index.php?start=<?php print($next_start); ?>">next page</a> &#187;

<?php
} 
elseif ((($_REQUEST["start"] > 0) && (($next_start - $photos_per_page) > $total_photos)) or ($_REQUEST["start"] < 0)){ 

//covering all bases

?>

  nav error.




<?php
} 
elseif(($_REQUEST["start"] == 0) && ($next_start > $total_photos)){ 

//you're in a photo gallery with only one page of photos

?>
  _
<?php
} 

else { 

//you're at the end of the photo galley

?>

  &#171;
  <a href="archives/pixels/photographs/index.php?start=<?php print($prev_start); ?>
  ">prev page</a>

<?php
}
?>

    Hi,

    You can directly acces individual values of an array. In your case you have the array:

    $files

    Say, you want the first foto from this arary, you use $files[0] to get that name.
    Along these lines..

    $start = $request["start"];
      if($start == '') 
       {
       $start=0;
       }
       else
       {
        if($start >= $total_photos)
           {
            //Throw some error or reset to $start = 0
           } 
    
    $photos_per_page = 12;
    $last = $start + $photos_per_page;
    
    if(! $last <= $total_photos )
      {
       // Throw some error and/or reset
      $last = $total_photos ;
      }
    
    
    for($i=$start; $i<=$last; $i++)
      {
      echo $files[$i];
      }
    

      Yes, but the values in this array are not 1, 2, 3, 4, etc. but instead are the results of filemtime.

        Hi,

        OK..
        -> The $i does not refer to the value but to the array-location-pointer. IF you have stored the name there, and placed the timestamp as a value, it get dificult. What you could of course consider.. If you need both the name and the time it to create 2 arrays, or a multi-dimensional array. Then use the routine suggested above?

        Or am I missing some fundamental reasoning for your method here?

        J

          Ok, I figured out something that works, albeit probably not that efficiently:

          <?php
          if(!isSet($_REQUEST["start"])){
                $_REQUEST["start"] = 0;
          }
          $i = $_REQUEST["start"];
          $photos_per_page = 12;
          $prev_start = $_REQUEST["start"] - $photos_per_page;
          $next_start = $_REQUEST["start"] + $photos_per_page;
          $u = 0;
          
          $exts = array("png","gif","jpg");
          $path = "/Users/harrison/Sites/images/photos"; 
          $handle = opendir($path); 
          $files = array(); 
          
          
          while ($file = readdir($handle)) {
          	if(in_array(substr(strtolower($file),-3),$exts)) {
          		$files[] = $file;
          		$times[] = filemtime($path."/".$file);
          	}
          }
          
          $total_photos = count($files);
          closedir($handle);
          arsort($times);
          $order = array_keys($times);
          foreach($order as $o) {
          
          if ($u >= $i && $u < $next_start && $u <= $total_photos && $u >=0) {
          echo "<div class='gallery'>\n<a href='images/photos/display.shtml/".$files[$o]."' class='newWindow'>\n<img alt='".$files[$o]."' class='gallery' src='images/photos/thumbnails/".$files[$o]."' />\n</a>\n</div>\n\n"; 
          $u++;
          }
          else {
          $u++;
          }
          }
          ?>

          thanks for the help!

            Write a Reply...