Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    May 2007
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    directory based image gallery assistence

    i am try to create a simple script that will read a directory, get the images in it and write them to the page. It would also seperate them in to multiple pages. This is what i have so far

    gal.php
    PHP Code:
    <?php
    $limit
    =5;

    function 
    returnimages($dirname) {
       
    $pattern="\.(jpg|jpeg|png|gif|bmp)$";
       
    $curimage=0;
       if(
    $handle opendir($dirname)) {
           while(
    false !== ($file readdir($handle))){
                   if(
    eregi($pattern$file)){
                     
    $galleryarray[$curimage]=$file;
                     
    $curimage++;
                   }
           }

           
    closedir($handle);
       }
       return(
    $galleryarray);
    }
    ?>
    page to display the images
    PHP Code:
    <?php
    include('gal.php');
    if(isset(
    $_GET['year'])){$year=$_GET['year'];}else{$year=2001;}
    $image = array();
    $image returnimages('IFAIphotos/'.$year);
    $limit 5;
    $images count($image);
    $totalrows ceil($images/2);
    $numofpages ceil($totalrows $limit);
    if(isset(
    $_GET['page'])){$page=$_GET['page'];}else{$page=1;}
    $limitvalue = ($page-1) * $limit;
    ?>

    <p align="center"><img src="IFAIphotos/<?=$year;?>/<?=$year;?>_logo.jpg" alt="IFAI <?=$year;?>" /></p>
    <?php
    for($i=1;$i==$images;$i++){
       echo 
    '<p align="center"><img src="IFAIphotos/'.$year.'/'.$image[$i].'" alt="1" width="150" hspace="5" />';
    $i++;
       echo 
    '<p align="center"><img src="IFAIphotos/'.$year.'/'.$image[$i].'" alt="1" width="150" hspace="5" /></p>';
    }
    ?>
    <p align="center"><span class="pagenav">
    <?php
    if($numofpages!=1){
       if(
    $page!=1){
    $pageprev = ($page-1);
       echo 
    '<a href="preevent.php?year='.$year.'&page='.$pageprev.'">PREV</a> ';
       }    
       for(
    $i 1$i <= $numofpages$i++){
       if(
    $i == $page){
       echo(
    $i." ");
       }else{
       echo(
    '<a href="preevent.php?year='.$year.'&page='.$i.'">'.$i.'</a> ');
        }        
       }
       if((
    $totalrows - ($limit $page)) > 0){
    $pagenext = ($page+1);
       echo(
    '<a href="preevent.php?year='.$year.'&page='.$pagenext.'">NEXT</a>');
       }else{
       echo(
    "");
            }
    }
    ?>
    basically the problems i am having are
    1. i can not seem to get the filename for each image. The for loop to echo the images displays nothing
    2. I know how to do it using a database driven system but am stumped on how to limit the rows of images per page for it to be pageinated.

    can someone please lend some assistence on this

    thanks

  • #2
    Regular Coder
    Join Date
    Jul 2007
    Location
    UK
    Posts
    223
    Thanks
    0
    Thanked 14 Times in 14 Posts
    The loop should be defined as:
    PHP Code:
    for($i=1;$i<=$images;$i++){ 
    So it loops if $i is less than or equal to $images. You were telling it to loop only if $i equaled $images.

    Also you might want to loose the "<p align="center">" from the second echo in the for loop, if you want the images to be displayed side by side.

    Also can you explain how you intend to use the following variables:
    PHP Code:
    $limit 5
    $images count($image); 
    $totalrows ceil($images/2); 
    $numofpages ceil($totalrows $limit); 
    Does $limit refer to the number of images you want displayed?

    To do the pagination you need to restrict the for loop by specifying a start and an end parameter which you need to work out depending on which page you're on.

  • #3
    New Coder
    Join Date
    May 2007
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts
    limit is rows 2 images per row 5 rows per page
    totalrows is how many rows will be needed
    numpaages is the number of pages needed

    but i think i got it know thanks
    Last edited by stickybomb; 08-15-2007 at 04:09 PM.

  • #4
    Regular Coder
    Join Date
    Jul 2007
    Location
    UK
    Posts
    223
    Thanks
    0
    Thanked 14 Times in 14 Posts
    Glad you worked it out, but since I think I've done it I thought I might as well post it!
    PHP Code:
    <?php 
    include('gal.php');
    if(isset(
    $_GET['year'])){$year=$_GET['year'];}else{$year='2001';} 
    $image = array(); 
    $image returnimages($path.$year); 
    $limit 5//number of rows
    $images count($image); 
    $totalrows ceil($images/2); //2 is the number of images per row.
    $numofpages ceil($images / ($limit*2)); 
    if(isset(
    $_GET['page'])){$page=$_GET['page'];}else{$page=1;} 
    $startlimit = ($page-1) * $limit*2;
    $endlimitvalue = ($page) * $limit*2
    ?> 
    <p align="center"><img src="IFAIphotos/<?=$year;?>/<?=$year;?>_logo.jpg" alt="IFAI <?=$year;?>" /></p> 
    <?php 
    for($i=$startlimit$i<$endlimitvalue$i++){ 
     if(!
    array_key_exists($i,$image)){
      break;
     }
     echo 
    '<p align="center"><img src="IFAIphotos/'.$year.'/'.$image[$i].'" alt="'.$image[$i].'" width="150" hspace="5" />'
     
    $i++;
     if(!
    array_key_exists($i,$image)){
      break;
     }
     echo 
    '<img src="IFAIphotos/'.$year.'/'.$image[$i].'" alt="'.$image[$i].'" width="150" hspace="5" /></p>'."\n"

    ?> 
    <p align="center"><span class="pagenav"> 
    <?php 
    if($numofpages!=1){ 
     if(
    $page!=1){ 
      
    $pageprev = ($page-1); 
      echo 
    '<a href="preevent.php?year='.$year.'&page='.$pageprev.'">PREV</a> '
     }     
     for(
    $i 1$i <= $numofpages$i++){ 
      if(
    $i == $page){ 
       echo(
    $i." "); 
      }else{ 
       echo(
    '<a href="preevent.php?year='.$year.'&page='.$i.'">'.$i.'</a> '); 
      }         
     } 
     if((
    $totalrows - ($limit $page)) > 0){ 
      
    $pagenext = ($page+1); 
      echo(
    '<a href="preevent.php?year='.$year.'&page='.$pagenext.'">NEXT</a>'); 
     }else{ 
      echo(
    ""); 
     } 

    ?>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •