Page 1 of 1

PHP use in HTML

Posted: Mon Dec 21, 2009 8:07 am
by hero789
Hi
I earlier posted regarding getting an image from a mysql database and displaying it to screen in a php form. Instead I have added the directory path. I am now trying to place the image in html. All the tabled data is displayed appropriately except for the image.
Any help appreciated.

<html>
<head>
<link rel="stylesheet" type="text/css" href="http://localhost/project/website/css/homepage.css"/>
</head>
<body>
<?php
//include the database connection file
include ($_SERVER['DOCUMENT_ROOT'].'\project\website\connect\databaseconnect.php');

$query = "select title, first_name, last_name, house_number, street, town, county, postcode, contact_number, emailaddress, username, password, image_title, image_file, image_type, image_size from trainer";
$result = mysql_query($query);

while ($row = mysql_fetch_array($result))
{
$title = $row['title'];
$firstname= $row ['first_name'];
$lastname = $row ['last_name'];
$house_number = $row['house_number'];
$street = $row ['street'];
$town = $row ['town'];
$county = $row ['county'];
$postcode = $row ['postcode'];
$telephone = $row ['contact_number'];
$email = $row ['emailaddress'];
$username = $row ['username'];
$password = $row ['password'];
$image_title = $row['image_title'];
$image_file = $row['image_file'];
$image_type = $row['image_type'];
$image_size = $row['image_size'];



echo "<table border='1'>";
echo "<tr>";
echo "<td>". $title ."</td>";
echo "<td>". $firstname . "</td>";
echo "<td>". $lastname ."</td>";
echo "<td>". $house_number."</td>";
echo "<td>". $street . "</td>";
echo "<td>". $town . "</td>";
echo "<td>". $county. "</td>";
echo "<td>". $postcode. "</td>";
echo "<td>". $telephone. "</td>";
echo "<td>". $email. "</td>";
echo "<td>". $username. "</td>";
echo "<td>". $password. "</td>";
echo '<td><img class="small" src="readfile($image_file)"/></td>';

echo "</tr>";
echo "</table>";
}

?>
<form>
<table>
<tr align="center">
<td>
<a href="http://localhost/project/website/admin/ ... htm">Admin Page</a>
</td>
</tr>
</table>
</form>
</body>
</html>

Re: PHP use in HTML

Posted: Mon Dec 21, 2009 8:18 am
by requinix

Code: Select all

echo '<td><img class="small" src="readfile($image_file)"/></td>';
The image source is supposed to be a URL. You gave the browser the raw image data. Can't do it that way.

You can probably use the $image_file in the src:

Code: Select all

echo '<td><img class="small" src="', $image_file, '"/></td>';
but it depends what $image_file actually has - full path to the image or just a short relative one?

Re: PHP use in HTML

Posted: Mon Dec 21, 2009 8:40 am
by hero789
Hi

Thanks for the post. I added it to my file. However now I get rather than a red x, a small blue/green image, not the image from the directory. The content of $image_file is an absolute path a copy of which is displayed. If that helps.

$path = ($_SERVER['DOCUMENT_ROOT'].'\project\website\admin\trainer\trainer_images\image.jpg');

<html>
<head>
<link rel="stylesheet" type="text/css" href="http://localhost/project/website/css/homepage.css"/>
</head>
<body>
<?php
//include the database connection file
include ($_SERVER['DOCUMENT_ROOT'].'\project\website\connect\databaseconnect.php');

$query = "select title, first_name, last_name, house_number, street, town, county, postcode, contact_number, emailaddress, username, password, image_title, image_file, image_type, image_size from trainer";
$result = mysql_query($query);

while ($row = mysql_fetch_array($result))
{
$title = $row['title'];
$firstname= $row ['first_name'];
$lastname = $row ['last_name'];
$house_number = $row['house_number'];
$street = $row ['street'];
$town = $row ['town'];
$county = $row ['county'];
$postcode = $row ['postcode'];
$telephone = $row ['contact_number'];
$email = $row ['emailaddress'];
$username = $row ['username'];
$password = $row ['password'];
$image_title = $row['image_title'];
$image_file = $row['image_file'];
$image_type = $row['image_type'];
$image_size = $row['image_size'];



echo "<table border='1'>";
echo "<tr>";
echo "<td>". $title ."</td>";
echo "<td>". $firstname . "</td>";
echo "<td>". $lastname ."</td>";
echo "<td>". $house_number."</td>";
echo "<td>". $street . "</td>";
echo "<td>". $town . "</td>";
echo "<td>". $county. "</td>";
echo "<td>". $postcode. "</td>";
echo "<td>". $telephone. "</td>";
echo "<td>". $email. "</td>";
echo "<td>". $username. "</td>";
echo "<td>". $password. "</td>";
echo '<td><img class="small" src="', $image_file, '"/></td>';

echo "</tr>";
echo "</table>";
}

?>
<form>
<table>
<tr align="center">
<td>
<a href="http://localhost/project/website/admin/ ... htm">Admin Page</a>
</td>
</tr>
</table>
</form>
</body>
</html>

Re: PHP use in HTML

Posted: Mon Dec 21, 2009 9:29 am
by hero789
If it helps this is the exact directory for the image
I:/xampp/htdocs/project/website/admin/trainer/trainer_images/.training.jpg

I clicked on the box that is displayed in the <img/> frame on my web page. It states that .training.jpg does not have a url, this i found by clicking on the image and then right clicking properties.

Re: PHP use in HTML

Posted: Mon Dec 21, 2009 9:48 am
by hero789
Success!
I used the following url and it has displayed the image. I was using the directory path. By adding "http://localhost/ it has solved it.
echo '<td><img class="small" src="http://localhost/project/website/admin/ ... jpg"/></td>';