Solved

Need to be able to add paging to this directory script

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP strings vs array errors 13 32
php56-php-mcrypt for rhel7 php56 1 50
Need to remove %20 from url in get 17 36
AJAX Wordpress Not Reading Variable 2 27
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 …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
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…

856 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