Solved

Need to be able to add paging to this directory script

Posted on 2007-03-19
4
193 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 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

Technology Partners: 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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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 count occurrences of each item in an array.

726 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