...

View Full Version : Help Needed Displaying a image in table format



msx112
01-04-2012, 06:32 PM
Hello all im rather new to php so go easy on me. Im trying to display data in a html table which is being gathered from my database. The part im stuck on is getting the image to show. The images are stored in a folder and inside the database it references the path name. Im struggling to see where ive gone wrong so any help is greatly appriciated, Thanks.





<?php
mysql_connect("pdb6.awardspace.com", "", "") or die(mysql_error());
mysql_select_db("") or die(mysql_error());

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

echo "<table border='1'>
<tr>
<th>Name</th>
<th>Cost</th>
<th>Availability</th>
<th>image</th>
</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['cost'] . "</td>";
echo "<td>" . $row['quantity'] . "</td>";
$image = $row["image"];
echo "<td>". "<img src='$image' alt='product' width='85' height='85'/>"."</td>";
echo "</tr>";
}
echo "</table>";

mysql_close($con);

?>

Riko15
01-04-2012, 06:54 PM
Hello all im rather new to php so go easy on me. Im trying to display data in a html table which is being gathered from my database. The part im stuck on is getting the image to show. The images are stored in a folder and inside the database it references the path name. Im struggling to see where ive gone wrong so any help is greatly appriciated, Thanks.





<?php
mysql_connect("pdb6.awardspace.com", "", "") or die(mysql_error());
mysql_select_db("") or die(mysql_error());

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

echo "<table border='1'>
<tr>
<th>Name</th>
<th>Cost</th>
<th>Availability</th>
<th>image</th>
</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['cost'] . "</td>";
echo "<td>" . $row['quantity'] . "</td>";
$image = $row["image"];
echo "<td>". "<img src='$image' alt='product' width='85' height='85'/>"."</td>";
echo "</tr>";
}
echo "</table>";

mysql_close($con);

?>

The image doesn't show up, because you can't just put a PHP variable into an echo. PHP will just say it's just a word with a dollar sign in front of it.

Here's you solution:




<?php
mysql_connect("pdb6.awardspace.com", "", "") or die(mysql_error());
mysql_select_db("") or die(mysql_error());

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

echo "<table border='1'>
<tr>
<th>Name</th>
<th>Cost</th>
<th>Availability</th>
<th>image</th>
</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['cost'] . "</td>";
echo "<td>" . $row['quantity'] . "</td>";
$image = $row["image"];
echo "<td>". "<img src='" . $image . "' alt='product' width='85' height='85'/>"."</td>";
echo "</tr>";
}
echo "</table>";

mysql_close($con);

?>


Have fun coding :thumbsup:

Spookster
01-04-2012, 07:04 PM
The image doesn't show up, because you can't just put a PHP variable into an echo. PHP will just say it's just a word with a dollar sign in front of it.


That is incorrect. Yes you can use variables inside strings being printed using echo as long as the strings use double quotes.

http://php.net/manual/en/language.types.string.php

Spookster
01-04-2012, 07:05 PM
Hello all im rather new to php so go easy on me. Im trying to display data in a html table which is being gathered from my database. The part im stuck on is getting the image to show. The images are stored in a folder and inside the database it references the path name. Im struggling to see where ive gone wrong so any help is greatly appriciated, Thanks.





<?php
mysql_connect("pdb6.awardspace.com", "", "") or die(mysql_error());
mysql_select_db("") or die(mysql_error());

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

echo "<table border='1'>
<tr>
<th>Name</th>
<th>Cost</th>
<th>Availability</th>
<th>image</th>
</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['cost'] . "</td>";
echo "<td>" . $row['quantity'] . "</td>";
$image = $row["image"];
echo "<td>". "<img src='$image' alt='product' width='85' height='85'/>"."</td>";
echo "</tr>";
}
echo "</table>";

mysql_close($con);

?>


Are you seeing broken images on the page? If so then the path is not correct. Look at the source code of the generated page and look at the path being created and it should be pretty obvious on what to do to fix it.

msx112
01-06-2012, 01:09 AM
Thanks for the help all sorted now :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum