Forum Moderators: coopster

Message Too Old, No Replies

Image in table problems.

         

fxb12xssi

2:11 pm on Mar 14, 2007 (gmt 0)

10+ Year Member



Hi,

I have a database table called photo which has various fields and one for filename.

The filename data is something like image1.jpg, image2.jpg...

Now in my php script, I am struggling to get the image to appear in the respective column of a table. Basically, I need the table field 'filename' to represent the filename of my images in the source directory and be printed in a column.

The query is
$SQL = "select * from photo";

if (!($photos = mysql_db_query($db,$SQL,$connection))) {
die (mysql_error());

the php code i'm using is:

while ($row = mysql_fetch_row($photos)) {
echo "<table width=\"100%\>";
echo "<tr><th>No</th>";
echo "<th>date</th>";
echo "<th>Filename</th>";
echo "<th>desc</th></tr>";
echo "<tr>";
foreach ($row as $key => $value) {
echo "<td width =\"25%\">";
print $value;
echo "</td>";
}
echo "</tr>";
echo "</table>";
}

Therefore, hopefully printing $value will print each row and hence each image in the right column. I believe i may need to store each filename in a <img> tag somewhere?

Any help would be appreciated,

Thanks

Scally_Ally

2:26 pm on Mar 14, 2007 (gmt 0)

10+ Year Member



you could just write it into an image tag on the fly, saving having to store it in the db.

<img src="images/myimages/<?php echo $value;?>">

Ally

grandpa

2:42 pm on Mar 14, 2007 (gmt 0)

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



Hi fxb12xssi, Welcome to WebmasterWorld

This might be closer to what you are wanting. I'm assuming that each $row result will contain the 4 values needed for each column.

while ($row = mysql_fetch_row($photos)) {
echo "<table width=\"100%\>";
echo "<tr><th>No</th>";
echo "<th>date</th>";
echo "<th>Filename</th>";
echo "<th>desc</th></tr>";

foreach ($row as $key => $value) {
if ($key == '0') echo "<tr><td width =\"25%\">$value</td>";
if ($key == '1') echo "<td width =\"25%\">$value</td>";
if ($key == '2') echo "<td width =\"25%\"><img src="images/myimages/" . $value . "></td>";
if ($key == '3') echo "<td width =\"25%\">$value</td></tr>";
}
}
echo "</table>";

fxb12xssi

3:01 pm on Mar 14, 2007 (gmt 0)

10+ Year Member



Thanks for the quick replies and cheers grandpa, your method works great!