...

View Full Version : directory based image gallery assistence



stickybomb
08-15-2007, 01:30 PM
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
$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
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 ;)

mcjwb
08-15-2007, 03:02 PM
The loop should be defined as:

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:

$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.

stickybomb
08-15-2007, 03:06 PM
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

mcjwb
08-15-2007, 03:17 PM
Glad you worked it out, but since I think I've done it I thought I might as well post it!

<?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("");
}
}
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum