While >> For ?

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
thiscatis
Forum Contributor
Posts: 434
Joined: Thu Jul 20, 2006 11:00 am

While >> For ?

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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
thiscatis
Forum Contributor
Posts: 434
Joined: Thu Jul 20, 2006 11:00 am

Post 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();
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$folderEntry won't be set until the second iteration.

Where's $x defined?
thiscatis
Forum Contributor
Posts: 434
Joined: Thu Jul 20, 2006 11:00 am

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