Page 1 of 1

Simple Iteration Question [SOLVED w/ answer]

Posted: Tue Oct 18, 2005 6:25 am
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.

Posted: Tue Oct 18, 2005 6:35 am
by feyd
Useful Posts wrote:Multi-column formatted output: PHP & MySQL formatting problem
may be of interest...

Posted: Tue Oct 18, 2005 7:45 am
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.

Posted: Tue Oct 18, 2005 7:47 am
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.

Posted: Tue Oct 18, 2005 8:40 am
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 :)