Solved

Need to be able to add paging to this directory script

Posted on 2007-03-19
4
187 Views
Last Modified: 2013-12-13
I have the following code that pulls all the images from a folder and shows them on screen (reducing them via a thumbnailer etc). How can I get this to be pageable. EG show 5 images per page with a  page 1, page 2, page 3 etc linking to the next images?

Many thanks
Neil

<?php
 // loop through images folder and obtain all details of allowed files
 $imgFolder = 'thumbs/';
 $pics=directory($imgFolder,"jpg,JPG,JPEG,jpeg,png,PNG");

 // write details to screen
 if ($pics[0]!="") {
  foreach ($pics as $p) {
   echo '<tr><td class="photoarea">';
   echo '<a href="'.$imgFolder.$p.'" class="clickme"> <img class="photos" src="thumbnail.php?gd=2&amp;src='.$imgFolder.$p.'&amp;maxw=100 "/>';
   echo '<br><span>Enlarge</span></a>';
   echo '</td><td class="phototext">';

   $thisImg = $imgFolder.str_replace('.jpg','.txt',$p);
   if (file_exists($thisImg)) {
    $fh = fopen($thisImg, 'r');
    $theData = fread($fh, filesize($thisImg));
    fclose($fh);
    echo $theData;
   } else {
    echo $p;
   }


   echo '</td></tr>';
  }
 }


 // -------------------------------------------------------------------------------------
 // functions
 // -------------------------------------------------------------------------------------
 function directory($dir,$filters) {
  $handle=opendir($dir);
  $files=array();
  if ($filters == "all"){while(($file = readdir($handle))!==false){$files[] = $file;}}
  if ($filters != "all") {
   $filters=explode(",",$filters);
   while (($file = readdir($handle))!==false) {
    for ($f=0;$f<sizeof($filters);$f++):
     $system=explode(".",$file);
     if ($system[1] == $filters[$f]){$files[] = $file;}
    endfor;
   }
  }
  closedir($handle);
  sort($files);  // sort by a-z
  return $files;
 }

 ?>
0
Comment
Question by:Neil Thompson
  • 2
  • 2
4 Comments
 
LVL 8

Expert Comment

by:netmunky
ID: 18749001
to clip a bit...

...
$s = $_REQUEST['s'] + 0;
for( $i = s; $i < 10 && $i < sizeof($pics); $i++ )
{
  $p = $pics[$i];
  ...
}
$l = sizeof( $pics );
calc_pnl( $s, $p, $n, $l, 10 );
?>
<a href="<?=$_SERVER['PHP_SELF']?>?s=0">first</a>
<a href="<?=$_SERVER['PHP_SELF']?>?s=<?=$p?>">prev</a>
<a href="<?=$_SERVER['PHP_SELF']?>?s=<?=$n?>">next</a>
<a href="<?=$_SERVER['PHP_SELF']?>?s=<?=$l?>">last</a>

<?php
function calc_pnl($s, &$p, &$n, &$l, $c = 20)
{
  $s = $s + 0;
  $n = $s + $c; $p = $s - $c;
  if($p<0){$p=0;}
  if($n>=$l){$n=$s;}
  $l-=$c;if($l<0){$l=0;}
  if($l<$n){$l=$n;}
}
?>

then you can just create links with ?s=5, etc
0
 
LVL 3

Author Comment

by:Neil Thompson
ID: 18755175
Cheers
I get the idea but cant fit it into the code above, it just loops constantly until the pc blue screens

Neil

<?php
// loop through images folder and obtain all details of allowed files
$imgFolder = 'thumbs/';
$pics=directory($imgFolder,"jpg,JPG,JPEG,jpeg,png,PNG");
$l = sizeof( $pics );
$s = $_REQUEST['s'] + 0;

echo 'l='.$l.'<br>';
echo 's='.$s.'<br>';

calc_pnl( $s, $p, $n, $l, 10 );

echo 'p='.$p.'<br>';
echo 'n='.$n.'<br>';
echo 'l='.$l.'<br>';

for( $i = s; $i < 10 && $i < sizeof($pics); $i++ )      {
      $p = $pics[$i];       
      // write details to screen
      echo '<tr><td class="photoarea">';
      //echo '<a href="'.$imgFolder.$p.'" class="clickme"> <img class="photos" src="thumbnail.php?gd=2&amp;src='.$imgFolder.$p.'&amp;maxw=100 "/>';
      echo 'pic:'.$p;
      echo '<br><span>Enlarge</span></a>';
      echo '</td><td class="phototext">';
      $thisImg = $imgFolder.str_replace('.jpg','.txt',$p);
            if (file_exists($thisImg)) {
                  $fh = fopen($thisImg, 'r');
                  $theData = fread($fh, filesize($thisImg));
                  fclose($fh);
                  echo $theData;
            } else {
                  echo $p;
            }
      echo '</td></tr>';
}
 
 // -------------------------------------------------------------------------------------
 // functions
 // -------------------------------------------------------------------------------------
 function directory($dir,$filters) {
  $handle=opendir($dir);
  $files=array();
  if ($filters == "all"){while(($file = readdir($handle))!==false){$files[] = $file;}}
  if ($filters != "all") {
   $filters=explode(",",$filters);
   while (($file = readdir($handle))!==false) {
    for ($f=0;$f<sizeof($filters);$f++):
     $system=explode(".",$file);
     if ($system[1] == $filters[$f]){$files[] = $file;}
    endfor;
   }
  }
  closedir($handle);
  sort($files);  // sort by a-z
  return $files;
 }
 
function calc_pnl($s, &$p, &$n, &$l, $c = 20) {
      $s = $s + 0;
      $n = $s + $c; $p = $s - $c;
      if($p<0){$p=0;}
      if($n>=$l){$n=$s;}
      $l-=$c;if($l<0){$l=0;}
      if($l<$n){$l=$n;}
}


 ?>
<a href="<?php echo $_SERVER['PHP_SELF']; ?>?s=0">first</a>
<a href="<?php echo $_SERVER['PHP_SELF']; ?>?s=<?php echo $p; ?>">prev</a>
<a href="<?php echo $_SERVER['PHP_SELF']; ?>?s=<?php echo $n; ?>">next</a>
<a href="<?php echo $_SERVER['PHP_SELF']; ?>?s=<?php echo $l; ?>">last</a>
0
 
LVL 8

Accepted Solution

by:
netmunky earned 500 total points
ID: 18755921
for( $i = s; $i < 10 && $i < sizeof($pics); $i++ )      {
$p = $pics[$i];      

that's not right

for( $i = 0; $i < 10 && $i + $s < sizeof($pics); $i++ )      {
$p = $pics[$i + $s];    
0
 
LVL 3

Author Comment

by:Neil Thompson
ID: 18756331
Thanks tons, this now works perfectly!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question