?
Solved

Need to be able to add paging to this directory script

Posted on 2007-03-19
4
Medium Priority
?
195 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

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