Forum Moderators: coopster
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>";
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
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:Note the single equal sign when assigning a value, and the double equal sign when comparing values.
$i = 0;
// Is the variable $i equal to zero?
if ($i == 0) {
echo "true";
} else {
echo "false";
}
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).
<?phpI 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 ...
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 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!
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.