Page 1 of 1

Using arrays in a photo gallery script

Posted: Wed Apr 30, 2003 5:45 am
by lunartek
In a previous post I talked about issues displaying images in a photo gallery script, it was solved at the time however. But had to sort the files, therefor needed using arrays so I rewrote the script a bit. And a new problem has came up.

If you have for example 6 images per page and click on 'next' link, you will get the next 6 images but the old one remains on the page so there are 12 images.

I'm not quite sure how to go about this when i'm using arrays, do I need to use array_chunks? I've tried but can't figure out how to get it work so any help would be good.

Thanks
--lunartek

Code: Select all

if (file_exists($_SERVER['DOCUMENT_ROOT']."$dirname")){
$dir = opendir($_SERVER['DOCUMENT_ROOT']."$dirname");
$i=1;
$count=0;
$increment = $rows*$cols-1;
$begin = (!empty($_GET['show']) && is_numeric($_GET['show']) && $_GET['show'] > 0) ? $_GET['show'] : 1;
$end = $begin + $increment;

$filearr = array();
while ($filename = readdir($dir)) {
if (ereg ("(.*)\.gif", $filename)) {
$filearr[] = $filename;
}
}
closedir($dir);
sort($filearr);
reset($filearr);


if (count($filearr) > 0){
echo "<table border=0 cellspacing=0 cellpadding=2>";
for ($i>=$begin; $i<=$end; $i++){

$imagesize = getimagesize($_SERVER['DOCUMENT_ROOT']."$dirname"."$filearr[$count]");
if ($imagesize[0]=="$width" && $imagesize[1]=="$height"){
echo "<td><img src="http://".$_SERVER['SERVER_NAME']."$dirname"."$filearr[$count]"."" width=$width height=$height></td>";
$count++;
if ($i%3==0){ echo "<tr></tr>"; }
}

}
echo "</table>";
}
echo "<BR><a href="".$_SERVER['PHP_SELF']."?"."&show=".($i)."">next</a>";

Posted: Wed Apr 30, 2003 9:54 am
by pootergeist
$filearr[$count]

should be

$filearr[$i]

in both instances that it occurs.

Posted: Thu May 01, 2003 3:00 am
by lunartek
I tried that but didn't change anything. I can't figure out what i'm missing here, any other ideas?


--lunartek

Posted: Thu May 01, 2003 3:18 am
by pootergeist
try echoing $begin after it is instantiated - maybe the ternery test isn't working properly

Posted: Thu May 01, 2003 3:25 am
by lunartek
Not sure what you're exactly asking but I assume you want to know what the $begin prints. On first page it shows 1, when 'next' is clicked it shows 7.

Posted: Thu May 01, 2003 3:32 am
by []InTeR[]
Try changing

Code: Select all

for ($i>=$begin; $i<=$end; $i++)&#123;
to

Code: Select all

for ($i=$begin; $i<=$end; $i++)&#123;

Posted: Thu May 01, 2003 3:35 am
by lunartek
That fixed it! :) I tried that before but it messed it up, but must have been because I had $count instead of $i at wrong place.

Thanks for all your help, it's appreciated.
--lunartek