Page 1 of 1

while loop not working

Posted: Thu May 13, 2010 6:48 am
by comports
Hi all - first post from me. I learnt php some time ago and just refreshing myself as something has gone wrong with my page. I wonder if you can help..??

I have a while loop that checks how many images in a folder and then displays a slideshow. It shows the first image, adds 1 to a $num variable and refreshes the page (it should show number 2 image until it reaches variable $total (total files in the folder). It shows the first fine, waits the 3 seconds but then jumps straight to the last image, missing out the ones in the middle.

Can be seen here http://photos.comports.net/?folder=pics/Our%20Wedding/ and click on slideshow.

I have also shown the code

Code: Select all

<?php
session_start();
error_reporting(0);
print"<HTML>
<HEAD>
<TITLE>PhotStore Slide Show</TITLE>
<META NAME=\"Generator\" CONTENT=\"EditPlus\">
<META NAME=\"Author\" CONTENT=\"Ashley Byrne\">
<META NAME=\"Keywords\" CONTENT=\"Image Gallery\">
<META NAME=\"Description\" CONTENT=\"\">
<link rel=\"stylesheet\" href=\"style2.css\">";
print"</HEAD><BODY background=\"slideback.gif\"><center><table border=0 cellspacing=0 cellpadding=0 width=\"100%\" height=\"100%\" bgcolor=\"#669966\">";
$pwd = $_SESSION['pwd'];
$num = $_GET["num"];
$show= $_GET["show"];
$folder = $_GET["folder"];
$dir = opendir($folder);
$i=1;
while ($a = readdir($dir)) {
if($a != "." && $a != ".."){
$files[$i] = $a;
++$i;
}
}
$total = count($files);

// resize the image if its too big
$maxwidth="600";
list($width, $height, $type, $attr) = getimagesize($folder.$files[$num]);
	if($width>$maxwidth){
		$widthpc = round(($maxwidth/$width)*100);
			$width = round(($width*$widthpc)/100);
			$height = round(($height*$widthpc)/100);
			$attr="width=".$width." height=".$height;
	}
	Else{
			$attr="width=".$width." height=".$height;
	}

print "<tr><td><center><b><font color=\"white\">".substr($folder,5,-1)." - ".substr($files[$num],0,-4)."</b>&nbsp;&nbsp;&nbsp;&nbsp;(". $num. " of ". $total.")</b></td></tr>";
print "<tr><td><center><img src=\"".$folder.$files[$num]."\" ".$attr." border=1><br>".$attr."</td></tr>";
print "<tr><td><br><center><a href=\"javascript:parent.window.close()\"><font color=\"white\">--|  End slideshow  |--</a></td></tr>";
while ($num < $total){
		print "next file is going to be = ".$num. " of ". $total;
print "<META HTTP-EQUIV=\"refresh\" content=\"3; URL=?folder=".$folder."&num=".$num."\"></td></tr></table>";
	$num = ($num+1);
	}
?>
Any help you can give would be appreciated. ps. I tried adding flush() before the final } but that made no difference.
Thanks
Ashley

Re: while loop not working

Posted: Thu May 13, 2010 7:35 am
by roders
1. Echo the count of that array.
2. Make sure that you're incrementing the value correctly.

Re: while loop not working

Posted: Thu May 13, 2010 9:25 am
by comports
Thanks Roders

I have added this in the code

Code: Select all

while ($num < $total){
$num = ($num+1);
		print "Next file is going to be = ".$num. " of ". $total;
print "<META HTTP-EQUIV=\"refresh\" content=\"3; URL=?folder=".$folder."&num=".$num."\"></td></tr></table>";
	flush();
	}
And it shows the total files (from the array above) which is fine, and it shows current file ($num) which is correct and then the next file will be $num +1, again correct. But it never does this. It just seems to run the full while statement and then refreshes the page but returns the last image not the next. Once refreshed it shows image 80 of 80 for example..

Re: while loop not working

Posted: Fri May 14, 2010 5:21 am
by roders
Try this script i built it in a hurry. Just try it out if it works then try and adapt it for ur needs.

Code: Select all

<?php
session_start();
if(isset($_GET['startagain']))
{
	$_SESSION['num']=1;
}
?>
<html>
<head>
<?php

if($_SESSION['num']<=9)
{	
	$_SESSION['num'] = $_SESSION['num']+1;
	$meta="refresh";
}else{
       $meta="";
}
print "<META HTTP-EQUIV=\"".$meta."\" content=\"3; URL=?num=".$_SESSION['num']."\"></td></tr></table>";
flush();

              
?>
 <title>Title</title>
 </head>
 <body>
 <A href="?startagain&num=1">Restart Slide</A>
 </body>
 </html>