Simple Iteration Question [SOLVED w/ answer]

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
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Simple Iteration Question [SOLVED w/ answer]

Post by jayshields »

Hi guys,

I'm really stumped at this and I know there must be a very simple solution.

I'm reading files from a dir and then printing each one into a table, I want 2 files to one row, so I need to echo <tr> and </tr> before and after every other file is printed into a column.

Here is my code:

Code: Select all

//Start the table
echo '<table border="1" bordercolor="black">';

/*Initialise a new row determination variable
$newrow = "yes";*/

//Read all the files from the directory and print them all in the table
if ($handle = opendir('gallery/')) {
   while (false !== ($file = readdir($handle))) {
       if ($file != "." && $file != "..") {
			/*if ($newrow == "yes") {
				echo '<tr>';
				$newrow = "no";
			}*/

			echo '<td><center><a href="' . $file . '"><img src="gallery/' . $file . '" border="0"></a><br><i>' . $file . '</i></center></td>';
			
			/*if ($newrow == "no") {
				echo '</tr>';
				$newrow = "yes";
			}*/
		}
   }
   closedir($handle);
}

//Close the table
echo '</table>';
You can see where I've attempted to implement something to do it, but it does it everytime a file is shown... lol. So I thought about changing it, but it gets too complex and hurts my brain.

Is there a simple way of doing something like this using the ternary operator?

Cheers.
Last edited by jayshields on Tue Oct 18, 2005 8:41 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Useful Posts wrote:Multi-column formatted output: PHP & MySQL formatting problem
may be of interest...
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

Thanks and sorry for not finding it first...

I followed what it said and tried this:

Code: Select all

//Start the table
echo '<table border="1" bordercolor="black">';

//Initialise some variables to help with printing the table
$rowmax = 2;
$x = 0;

//Read all the files from the directory and print them all in the table
if ($handle = opendir('gallery/')) {
   while (false !== ($file = readdir($handle))) {
       if ($file != "." && $file != "..") {
			if ($x % $rowmax == 0) {
				echo '<tr>';
			}
			echo '<td><center><a href="' . $file . '"><img src="gallery/' . $file . '" border="0"></a><br><i>' . $file . '</i></center></td>';
			
			if ($x % $rowmax == $rowmax - 1) {
				echo '</tr>';
			}
			$x++;
		}
   }
   closedir($handle);
}

//Close the table
echo '</table>';
and it was a total guess, but It worked straight away!

I don't understand it though; what does

Code: Select all

$x % $rowmax == 0
actually mean? It's just the percentage operator that I don't understand... I searched Google to no avail.

Thanks again.
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

actually it'sthe modulas operator.. 3 % 2=1, 4%2=0, get it?:-D
essentially after you get two columns start a new row is what it's doing in your code i do believe?

edit: more thourough explanation, 3 divided by 2 is 1 with a remainder of 1 and 4 divided by 2 is 2 with a remainder of 0.hence, my answers.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

aahhh i understand now, read your post 3 times!

so it just finds the remainder of a division between 2 integers.

weird way to work out what i did in my code then, must have been a clever one that came up with that idea!

thanks :)
Post Reply