Hi Rob,
Sounds like you grabbed that piece of code from nowhere without understanding it, am I right ? . Don't worry, you can be ashamed but I will help you though :p
First you have to understand what a HTML TABLE is. It's made up by cells (columns and rows). That script is very tricky, it uses $left as a column and row switcher. If $left is true then it's column 1, then it's set to false, so it's column 2... back to 1, it's a new row and column 1 again.
I don't like that script 🙂. The most logical way is to use a REAL column counter. This way it can work for 2, 3 or n columns:
$NbCol = 4; // in your case you want 4 columns but you can set it to 2, 3, 5 or more
$c = 1; // our column counter, first column index is 1 (column 1)
Each time a new row is created we have to insert a <tr> tag. For each column, we add a <td> tag. The hard part is when the last column wasn't reached but that no more pictures are to be displayed ! It happens with 5 pictures, 4 pictures on the first row, last row only contain 1 picture ! Let's create the table first:
(note: I don't use echo but ?><?php to ouput plain HTML - better, faster... hum stronger :p)
?><table><?php
while ($row = mysql_fetch_array ($result))
{
// Don't use extract it's crappy, slower,... and useless !
// extract ()
// We have a new row if the current column counter is 1 !
if ($c == 1)
{
?><tr><?php
}
// Let's display our thumbnail
// <?= ?> is a very useful shortcut to quickly echo a PHP value !
?><td><img src="<?= $row ['filename'] ?>"></td><?php
// Next column, so new row ?
$c ++;
if ($c > $NbCol)
{
// New row so let's close the previous </tr> tag
?></tr><?php
// Column 1 again...
$c = 1;
}
}
That's it. But something is missing. Remember what we talked about... 5 pictures, 4 columns... so the last row only has 1 picture ? It means our <tr> wasn't closed ! That's why there was a "if (!$left) echo '<td> </td></tr>';" after the while in your script. Here we just have to check that current column is between 2 and 4. If $c is equal to 2, it means that 1 picture was displayed (because of the $c ++ that ALWAYS occurs)... same goes for 2 pictures, so column 3...
if (($c > 1) && ($c < 5))
{
?></tr><?php
}
And voilà . It should work perfectly (not tested but you will :p).
A few last advices:
- don't use extract
- don't use echo to output PLAIN HTML, use ?><php to escape from PHP to HTML and <?= ?> to ouput your PHP values (shortcut)
- don't use tricks like the $left, when it comes to 3 columns, it doesn't work. Tricks are for sub-level-beginners 😃 (I hope it's not your script sorry ')
- don't collapse your code, use {}, returns, tabulations... Mine is longer but far far easier to read and understand
I hope you agree and that It will work well!
JM