Debt Help - Loans - Sexy Halloween Costumes - Credit Card Consolidation - Remortgages

PDA

View Full Version : Ask script to echo n images per row


junandya
Jan 1st 2008, 5:27 am
Hi all,

i have some problem with "show images". This is the field from table 'gallery',

id int(25)
imagePath varchar(250)
imageTitle varchar(250)

which is the image path taken from field 'imagePath', so i could be like this: <img src="imgGallery/<?PHP $row["imagePath"]; ?>">

The result that i want is like this, all images displayed in each row is 4 images:
<TABLE>
<TR>
<TD>
<TABLE>
<TR>
<TD><img src="imgGallery/1.jpg"></TD>
</TR>
<TR>
<TD>tile: image number 1</TD>
</TR>
</TABLE>
</TD>
<TD>
<TABLE>
<TR>
<TD><img src="imgGallery/2.jpg"></TD>
</TR>
<TR>
<TD>tile: image number 2</TD>
</TR>
</TABLE>
</TD>
<TD>
<TABLE>
<TR>
<TD><img src="imgGallery/3.jpg"></TD>
</TR>
<TR>
<TD>tile: image number 3</TD>
</TR>
</TABLE>
</TD>
<TD>
<TABLE>
<TR>
<TD><img src="imgGallery/4.jpg"></TD>
</TR>
<TR>
<TD>tile: image number 4</TD>
</TR>
</TABLE>
</TD>
</TR>
<TR>
<TD>
<TABLE>
<TR>
<TD><img src="imgGallery/5.jpg"></TD>
</TR>
<TR>
<TD>tile: image number 5</TD>
</TR>
</TABLE>
</TD>
<TD>
<TABLE>
<TR>
<TD><img src="imgGallery/6.jpg"></TD>
</TR>
<TR>
<TD>tile: image number 6</TD>
</TR>
</TABLE>
</TD>
<TD>
<TABLE>
<TR>
<TD><img src="imgGallery/7.jpg"></TD>
</TR>
<TR>
<TD>tile: image number 7</TD>
</TR>
</TABLE>
</TD>
<TD>
<TABLE>
<TR>
<TD><img src="imgGallery/8.jpg"></TD>
</TR>
<TR>
<TD>tile: image number 8</TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>

My question is how is the complete php script (also the query), so i can display the images as 4 images in a row, and 4 images again in next row...

Thank you

phpl33t
Jan 1st 2008, 10:14 am
I will code this for $25.

Brewster
Jan 1st 2008, 2:28 pm
Personally, I think your nested tables will bloat your html. I would try something like this:

<?php

$result = mysql_query( "SELECT * FROM gallery" );

$count = 0;

while ( $Row = mysql_fetch_assoc( $result ) )
{
$count++;

echo '<TABLE style="float:left;">
<TR>
<TD><img src="imgGallery/' . $Row["imagePath"] . '"></TD>
</TR>
<TR>
<TD>tile: ' . $Row["imageTitle"] . '</TD>
</TR>
</TABLE>';

if ( $count % 4 ) echo '<br style="clear:both;" />';

}

?>

The table in the loop could be replaced by a div if need be. Have a play and see what you think.

Brew

junandya
Jan 1st 2008, 6:12 pm
@phpl33t : thanx for your offering, but i'm really sorry this i really dont have enough money for this code, thanx anyway.

@Brewster : thanx it's working properly, but i have to change
if ( $count % 4 )
to be like this
if ( $count % 4 == 0 )

:) Best Regards

phpl33t
Jan 2nd 2008, 5:26 pm
A fix for that code:



$result = mysql_query( "SELECT * FROM `gallery`") or die(mysql_error());
if (mysql_num_rows($result) >= 1) {
echo '<table border="0">';
$column = 1;
while($items = mysql_fetch_array($result)) {
$imagepath = stripslashes($items['imagepath']);
$imageTitle = stripslashes($items['imageTitle']);
if ($column == 1) {
echo '<tr>';
}
echo '
<td valign="top" width="50%" align="center">
<div><img src="./imgGallery/'.$imagePath.'"></div>
<div><strong>Title: '.$imageTitle.'</div>
</td>
';
$column++;
if ($column == 3) {
echo '</tr>';
$column = 1;
}
}
echo '</table>';
} else {
echo '<div>No images found in the database!</div>';
}