Using arrays in a photo gallery script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lunartek
Forum Newbie
Posts: 15
Joined: Thu Apr 24, 2003 1:10 pm

Using arrays in a photo gallery script

Post 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>";
pootergeist
Forum Contributor
Posts: 273
Joined: Thu Feb 27, 2003 7:22 am
Location: UK

Post by pootergeist »

$filearr[$count]

should be

$filearr[$i]

in both instances that it occurs.
lunartek
Forum Newbie
Posts: 15
Joined: Thu Apr 24, 2003 1:10 pm

Post by lunartek »

I tried that but didn't change anything. I can't figure out what i'm missing here, any other ideas?


--lunartek
pootergeist
Forum Contributor
Posts: 273
Joined: Thu Feb 27, 2003 7:22 am
Location: UK

Post by pootergeist »

try echoing $begin after it is instantiated - maybe the ternery test isn't working properly
lunartek
Forum Newbie
Posts: 15
Joined: Thu Apr 24, 2003 1:10 pm

Post 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.
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post 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;
lunartek
Forum Newbie
Posts: 15
Joined: Thu Apr 24, 2003 1:10 pm

Post 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
Post Reply