Page 1 of 1

While >> For ?

Posted: Sat Mar 17, 2007 8:08 am
by thiscatis
Hello,

I'm using a while loop to get all the content from a folder,
But I need to specify a for loop somewhere to do a certain action when reached
a specific file in the folder..
so
> getting the content with

Code: Select all

while($folderEntry=$folder->read())
> when reached file 4: do something
> when reached file 8: do something
etc..
But whatever I try it's either looping the same file over and over or
looping the same file 10 times and then moves on to the next one.

Posted: Sat Mar 17, 2007 8:31 am
by feyd
You don't need a for loop.

Depending on how you start counting and a few other things,

Code: Select all

$count % 4 == 0

Posted: Sat Mar 17, 2007 8:40 am
by thiscatis
Hello,

This gives me an infinitive loop i think.. don't know where the problem is:

Code: Select all

$total_images = // predefined before this snippet;
$row = 5;

$folder=dir("modules/shoots/$s_pics/");

	echo "<table cellpadding=5 cellspacing=5 width=90%>";
	
	for($i = 0; $i < $total_images; $folderEntry=$folder->read()){
		
				if($i % $row == 0 )
				{
				$output .= "<tr>\n";
				$output .= "<td style=\"width= 20%\"><img src=modules/shoots/thumbsup.php?image=$s_pics/$folderEntry&width=225 /></td>";
				$i++;
				}
				
				if($x % $row == $row - 1)
				{
				$output .= "\r</tr>\n\n";
				
				}
																}
		                           
		if($left = (($total_images + $row - 1) % $row))
		$output .= '<td colspan="' . $left . '">' . "</td>\n</tr>\n\n";

		$output .= "</table>\n\n";
	
		echo $output;
$folder->close();

Posted: Sat Mar 17, 2007 9:00 am
by feyd
$folderEntry won't be set until the second iteration.

Where's $x defined?

Posted: Sat Mar 17, 2007 9:10 am
by thiscatis
hmm. should be i,

I simplified the code to first get my table correct.
the code is now

Code: Select all

$howmany = count(glob("/homepages/6/d177884807/htdocs/members/modules/shoots/$s_pics/{*.JPG,*.jpg,*.png}", GLOB_BRACE));
$rowmax = 5;
$output .= "<table cellpadding=2 width=100%>\n\n";
$folder=dir("modules/shoots/$s_pics/");


	
	for($i = 0; $folderEntry=$folder->read(); $i++){
		
			if($i % $rowmax == 0)
				$output .= "<td>$i</td>";

			if($i % $rowmax == $rowmax - 1)
				$output .= "\r</tr>\n\n";

																}
if($left = (($howmany + $rowmax - 1) % $rowmax))
$output .= '<td colspan="' . $left . '">' . "</td>\n</tr>\n\n";

$output .= "</table>\n\n";

echo $output;																

$folder->close();
but this just gives me a list of 0 5 10 15 etc...

edit got it,
forgot
$output .= "<tr>\n";