Solved

Need to be able to add paging to this directory script

Posted on 2007-03-19
4
183 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:NeilT
  • 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:NeilT
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:NeilT
ID: 18756331
Thanks tons, this now works perfectly!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
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…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now