Forum Moderators: coopster

Message Too Old, No Replies

Newbie image gallery

how to make a simple automated gallery

         

guerillaSEO

9:36 pm on Mar 11, 2005 (gmt 0)

10+ Year Member



this is simple, real simple ive only been learning php for a couple of days!

any help would be appreciated, heres a summary:

i want to have 24 images on a page within a 6x4 table.
the images are saved in images with the convention example001, example002, example003 etc..

<?php
echo "<table>";
i == 0;
while (i <= 24) {
i++;
if (i <= 24) { echo "<tr>"; }
echo "<td><img alt="" src="images/example.jpg" /></td>";
if (i == 6) { echo "</tr>"; i = 0; }
}
endwhile;
?>

why am i getting this error?
Parse error: parse error, unexpected T_INC in *exemplified path* on line 13
echo "</table>";

kamkaz

10:49 pm on Mar 11, 2005 (gmt 0)

10+ Year Member



Just so you know there are a few image gallerys written in PHP, perl and other languages. Might not want to reinvent the wheel.

syndeticFT

12:20 am on Mar 12, 2005 (gmt 0)

10+ Year Member



I think all the variables should have $ signs.
Also I think that the close curly bracket for your if statements should have a semi colon after it unless there is an else or elseif comming next.

This bit will echo a <TR> every line because i is always less than 24.
if (i <= 24)
{
echo "<tr>";
}

I think it would be easier with 2 variables $x and $y
$i == 0; //initialise $i used with the file name
for ($y = 1; $y <= 4; $y++)
{
echo "<tr>"; // start the row
for ($x = 1; $x <= 6; $x++)
{
$i = $i + 1; // counts to 24 eventually
echo "<td><img alt="" src="images/example.jpg" /></td>";
// you will also need to have something to change the name of the file each time.
// The example you had and that I have continued with will send out the same file each time.
// sometjhing like echo "<td><img alt="" src="images/example$i.jpg" /></td>";
}; // note the semi colon to close the inner loop
// now back in the outer loop
echo "</tr>";
}; // close off the outer loop

coopster

2:21 am on Mar 12, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, guerillaSEO.

I'd like to add to syndeticFT's analysis. First, there is a difference between assignment [php.net] and equality [php.net].

// Set the variable '$i' to the value of zero: 
$i = 0;
// Is the variable $i equal to zero?
if ($i == 0) {
echo "true";
} else {
echo "false";
}
Note the single equal sign when assigning a value, and the double equal sign when comparing values.

Also, you may want to use a for [php.net] loop rather than the while loop. It just seems to "fit" better here. Lastly, be careful when you echo HTML statements containing double quotes, you may want to escape them (note the backslash character in the img alt attribute, that is called escaping a character).

<?php 
echo '<table><tr>';
for ($i = 0; $i <= 24; i++) {
// alternatively, you could use single quotes in this case:
//echo '<td><img alt="" src="images/example.jpg" /></td>';
echo "<td><img alt=\"\" src=\"images/example.jpg\" /></td>";
if ($i == 6) {
echo "</tr><tr>";
$i = 0;
}
}
echo '</tr>';
?>
I don't quite understand how you are trying to handle your table rows, but I tried to get you on the right track a bit ...

guerillaSEO

1:02 pm on Mar 12, 2005 (gmt 0)

10+ Year Member



ok i have been learning furiously over the last 24hours as i have been in bed with the flu for about a week!

i dont want to reinvent the wheel, but i wanna learn how to make the wheel for my own pleasure.

this is what ive got so far:

<?php
echo "<table>";
$i = 0;
$x = 1;
while ($i <= 16) {
$i++;
if ($x <= 4) { echo "<tr>"; }
echo '<td><img alt="" src="images/example.jpg" /></td>'; $x++;
if ($x = 4) { echo "</tr>"; $x = 1; }
}
echo "</table>";
?>

its a bit better, rows still dont work!

ergophobe

5:28 pm on Mar 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You need to close your row first, then start a new row. Also, life will be so much easier for you if each statement goes on its own line.

Disclaimer: none of this is tested, so there may be stupid typos and errors, but the ideas should be essentially right.

So taking your script and changing it as little as possible to get the desired effect:


echo "<table><tr>";
$i = 0;
$x = 1;
while ($i <= 16) {
$i++;
if ($x > 4)
{
echo "</tr><tr>";
$x = 1;
}
echo '<td><img alt="" src="images/example.jpg" /></td>';
$x++;
}
echo "</tr></table>";

Now, there are a number of things that you could do to simplify this even further such using a for loop [us2.php.net] and a modulus operator [us2.php.net]. This will eliminate the need to keep resetting $x.

A modulus operator basically gives you the remainder part of integer division, so
5 % 2 = 1
6 % 2 = 0

So now back to your script


echo "<table><tr>";
for ($i=0; $i< 16; $i++)
{
if (!$i % 4)
{
echo "</tr><tr>";
}
echo '<td><img alt="" src="images/example.jpg" /></td>';
}
echo "</tr></table>";

We've gone from 11 statements to 6 and we've eliminated one variable.
Notes:
- $i starts at 0 because we want a new row at 4, 8, 12 which ar really the 5th, 9th and 13th cells
- $i stops at < 16 (i.e. 15) because it's zero-indexed.
- we check for (!$i % 4) => "Not $i mod 4" => "Mod 4 is equal to zero" => "Four goes into the current value of $i without leaving a remainder". That's how we know it's time to start a new row.