|
-
May 17th, 2007, 07:12 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] rows display picture
I have this image source <img src=....> I want it to be displayed in a table that has as many rows as the picture in the database increases but limitted to five rows.
For exmaple, if the table has one picture,
then
Code:
<table>
<tr><td><img src....>
</td></tr></table>
But if the database has 2 pictures, then another row is automatically added to the page, that would be second row to store second picture.
-
May 17th, 2007, 09:53 PM
#2
Addicted Member
Re: rows display picture
Well I don't completely understand what you're trying to do but if it's what I think, then use a loop to display the pictures depending on how many there are (up to five):
PHP Code:
$numPics = count($pics);
if ($numPics > 5) {
for ($i = 0; $i <= 4; $i++) {
echo "<tr><td><img src=\"$pics[$i]\"></td></tr>";
}
} else {
for ($i = 0; $i <= $numPics - 1; $i++) {
echo "<tr><td><img src=\"$pics[$i]\"></td></tr>";
}
}
Just put the path to the pictures into the $pics array in any way you see fit, whether randomly or whatever, and have it display up to five of them.
Last edited by asterix299; May 17th, 2007 at 10:05 PM.
-
May 18th, 2007, 12:29 AM
#3
Thread Starter
Frenzied Member
Re: rows display picture
Not quite what I wanted. See I want a table that can list many photos in "rows" but each row can consist of only five photos. Which means, the first rows has five pictures, second row has five pictures and so on. ...
-
May 18th, 2007, 02:18 AM
#4
Re: rows display picture
a bit messy. you get the idea, though.
PHP Code:
<?php
$pictures = array("picture1", "picture2", "picture3", "picture4", "picture5", "picture6", "picture7");
$ii = 0; //index for the row
$cols = 5; //number of columns in each row
$ncol = true; //whether or not to make a new column
for($i = 0; $i <= count($pictures) - 1; $i++){
$ii++;
//end a row?
if($ii > $cols){
$ii = 0; //reset row index
echo '</tr>' . "\n";
$ncol = true;
}
//start a new row?
if($ncol == true){
$ncol = false;
echo '<tr>' . "\n";
}
//display image
echo ' <td><img src="' . $pictures[$i] . '" /></td>' . "\n";
//is the table finished?
if($i == count($pictures) - 1){
//finish filling the row if we need to
if($ii < $cols){
for($j = $ii; $j < $cols - 1; $j++){
echo ' <td> </td>' . "\n";
}
}
echo '</tr>' . "\n";
}
}
?>
Last edited by kows; May 18th, 2007 at 02:24 AM.
-
May 18th, 2007, 12:45 PM
#5
Thread Starter
Frenzied Member
Re: rows display picture
Kows, thank you for giving me the idea. I went ahead and wrote up my own code:
PHP Code:
<?php
$pictures = array("picture1", "picture2", "picture3", "picture4", "picture5", "picture6", "picture7", "picture8", "picture9", "picture10", "picture11");
$ii = 0; //index for the row
$cols =2; //number of columns in each row
for($i = 0; $i <= count($pictures) - 1; $i++)
{
// increment row index
$ii++;
if ($i ==0)
{
echo "<table border=0>\n<tr>\n";
}
echo "<td>$pictures[$i]</td>\n";
if ($ii >= $cols)
{
echo "</tr>\n<tr>\n";
$ii=0; //reset the index row
}
if ($i==count($pictures)-1)
{
echo "</tr></table>";
}
}
?>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|