Forum Moderators: coopster

Message Too Old, No Replies

PHP image display with hyperlink problem

PHP image hyperlink

         

crazy88888888

6:53 pm on May 17, 2011 (gmt 0)

10+ Year Member



Can anyone help why this image binary is not showing as an image. Here is what I have:

<?php
require_once('Connections/myConnection.php');

$query = mysql_query("SELECT * FROM imagetest ORDER BY RAND()LIMIT 1") or die(mysql_error());
while ($array = mysql_fetch_array($query, MYSQL_ASSOC)){
$image = $array['image'];
$address = $array['address'];
$name = $array['name'];

echo "<a href='$address' target='_blank'><img src='$image' alt='$name'></a>";
}
?>

Matthew1980

7:22 pm on May 17, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there crazy88888888,

Welcome to the forums!


<?php
require_once('Connections/myConnection.php');
$sql_query = "SELECT * FROM `imagetest` ORDER BY RAND() LIMIT 1";
$query = mysql_query($sql_query) or die(mysql_error());

//changed name so as not to confuse
while ($arrayImage = mysql_fetch_array($query, MYSQL_ASSOC)){

//assign vars
$image = $arrayImage['image'];
$address = $arrayImage['address'];
$name = $arrayImage['name'];

//see what your actually getting in that array
echo "<pre>";
print_r($arrayImage);
echo "</pre>";

echo "<a href=\"".$address."\" target=\"_blank\"><img src=\"".$image."\" alt=\"".$name."\"></a>";
}
?>


Hopefully this will clear things up for you :) I have tidied up the echo statement up so that it remains within the W3C remit.

Cheers,
MRb

crazy88888888

7:47 pm on May 17, 2011 (gmt 0)

10+ Year Member



Thanks for your reply Matt here is what I get:

Array
(
[id] => 3
[name] => Avtar Wellness
[image] => ÿØÿàJFIFHHÿá2http://ns.adobe.com/xap/1.0/



Adobe Fireworks CS4
2011-04-06T02:57:20Z
2011-05-10T20:37:25Z


image/jpeg


This doesn't look like my address is pulling for my hyperlink or at least the correct one should be [avtarwellness.com...]

crazy88888888

8:07 pm on May 17, 2011 (gmt 0)

10+ Year Member



Also Matt I am still seeing the binary and not the image do I need to put a header("Content-type: image/jpg"); ? Look at my link [sweetakes.com...] and you will see my problem. Thanks

crazy88888888

8:10 pm on May 17, 2011 (gmt 0)

10+ Year Member



header("Content-type: image/jpg"); Produces a blank screen.

Matthew1980

9:15 pm on May 17, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



header("Content-type: image/jpeg"); might do the trick, but yes, in answer to your question, you do need to instruct the browser to what will be coming to it.

Do these images have to be stored in the DB, and if so are you storing them as blob longblog types?

Other than that I am not sure what to suggest other than to check that the mime type is definitly what you say it is.

Cheers,
MRb

crazy88888888

9:33 pm on May 17, 2011 (gmt 0)

10+ Year Member



Yes, they are blobs on the database. All of my images will be under 250K and eventually have other queries that are attached to each ID. So I think the DB is best senario vs a file server. But for the life of me I don't know why the header("Content-type: image/jpg"); is not converting the binary. Wierd. I will keep trying. Thanks for your help.

agent_x

10:16 pm on May 17, 2011 (gmt 0)

10+ Year Member



What is it you're actually trying to do here, it seems a little confused? Are you trying to output binary as an image file, or are you trying to display that image in an HTML page? You can't do both in the same script.

coopster

3:49 pm on May 18, 2011 (gmt 0)

crazy88888888

12:53 am on May 19, 2011 (gmt 0)

10+ Year Member



To agent_x and Coopster:

This is what I am trying to do. I will have a website where individuals will register their information and put it to a database. The information will be a address (http://mycompany.com, a name (mycompany) and a image (image will be stored as a blob on database. All of these will be stored in a database. Then on my website these photos will be advertisements at top when page loads. My php will randomly choose an image from the database associated with the address and name. Right now I can print or echo the image in a random order each time page refreshes but the image doesn't target when clicked to domain of the company. I can get the binary to show up and when I click on the binary and it links to the domain but when I put in a header("Content-type: image/jpeg"); it produces a blank screen without the header it just shows the clickable binary. So I need to know how to insert an a href and show the image. I have checked all browsers and they all react the same. Please advise ...

coopster

2:40 am on May 19, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Create your page with your html and every place you need an image you can link back to a PHP script that merely gets the image from the database and sends back the appropriate content type header and then binary image file. Did you read the linked page? The link offered shows how to accomplish the task you request.

crazy88888888

5:48 am on May 19, 2011 (gmt 0)

10+ Year Member



Forgive me if I have not fully explained myself. But I have created multiple pages that these ads are on and I have created the script that pulls the images from the database with the appropriate content type here is the code for that:

<?php
require_once('Connections/myConnection.php');


$image = mysql_query("SELECT * FROM imagetest ORDER BY RAND()LIMIT 1");
$image = mysql_fetch_assoc($image);
$image = $image['image'];

header('Content-type: image/jpg');
echo $image;

?>

That code works fine for just pulling the image from the database but it only provides the image and not the target url when image is linked. When I try and use this same code with an a href attached and try to echo it I get a blank screen no image no data of any kind. Here is the code:

<?php
require_once('Connections/myConnection.php');


$image = mysql_query("SELECT * FROM imagetest ORDER BY RAND()LIMIT 1");
$image = mysql_fetch_assoc($image);
$image = $image['image'];

header('Content-type: image/jpg');
    echo "<a href=\"".$address."\" target=\"_blank\"><img src=\"".$image."\" alt=\"".$name."\"></a>";

?>

** I have only been coding for a couple weeks so I don't have a vast knowledge of PHP and how it integrates with HTML, but I am learning. From what I have read and studied it seems to me that this should work. Thanks for any further help...

agent_x

11:47 am on May 19, 2011 (gmt 0)

10+ Year Member



No, as I said you can't do both things in the same script. You can either output an image or some HTML, not both. So you need two scripts, one to output an image, and one to output the HTML that displays that image using an img tag.

crazy88888888

4:41 pm on May 19, 2011 (gmt 0)

10+ Year Member



Ok, now how do I do that. I have the script to output the image, do I need another hyperlink.php page that has the echo "<a href=\"".$address."\" target=\"_blank\"><img src=\"".$image."\" alt=\"".$name."\"></a>"; in it and then on my html page call them both. If it is a random query how do I have both scripts have the same random query ID?

rocknbil

5:30 pm on May 19, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



1. Set up your script to output images only. Example, output_image.php.

2. Set up your display script and it has something like this.


$img_script = '/url/to/output_image.php'; // URL, NOT PATH like an include

echo "<img src=\"$img_script?img_id=" . $row['id'] . "\" alt=\"" . $row['pagetitle'] . "\">";

agent_x

6:02 pm on May 19, 2011 (gmt 0)

10+ Year Member



Ok, now how do I do that. I have the script to output the image, do I need another hyperlink.php page that has the echo "<a href=\"".$address."\" target=\"_blank\"><img src=\"".$image."\" alt=\"".$name."\"></a>"; in it and then on my html page call them both. If it is a random query how do I have both scripts have the same random query ID?


You will need to change your queries slightly.

Your image output script needs to accept a query parameter that tells it which image to pull from the database. That would correspond to the ID column in the table. Suppose it was passed in as GET parameter "imageid", your code would look like

$imageid = $_GET["imageid"];
$query = mysql_query("SELECT image FROM imagetest WHERE id=$imageid") or die(mysql_error());

That will get the image data out of the table when you know the ID of the image.

To display the image, you need a new script that will output the HTML, and the query for this will look like

$query = mysql_query("SELECT id,name,address FROM imagetest ORDER BY RAND()LIMIT 1") or die(mysql_error());


Rather than explicitly assigned variables from the result set, I always just use extract(), hence

if($row = mysql_fetch_assoc($query)
{
extract($row);
echo "<a href='$address' target='_blank'><img src='/path/to/imagescript.php?imageid=$id' alt='$name'></a>";
}

Now you can see how using the first script as the "image" in the src attribute of your HTML img tag works, by passing in the id of the image in the database. But note that it would probably be far simpler at this point to have the images stored as files in some directory within the document tree, then there would be no need to call a separate script to display them, and also less load on the MySQL server in pulling them from the table - which it has to do twice the way you are doing it.

crazy88888888

7:12 pm on May 25, 2011 (gmt 0)

10+ Year Member



To AgentX:

Ok this is what I have:

the php for the image is called getimage.php and it looks like this:

<?php
require_once('mysql_connect.php');

$id = $_GET["id"];

$query = mysql_query("SELECT id FROM imagetest WHERE id=$id") or die(mysql_error());

echo "$imageid";
?>

Now the other page I have is called rotatead.php and it looks like this:

<?php
require_once('mysql_connect.php');


$query = mysql_query("SELECT id,image,name,address FROM imagetest ORDER BY RAND()LIMIT 1") or die(mysql_error());

if($row = mysql_fetch_assoc($query)
{
extract($row);
echo "<a href='$address' target='_blank'><img src='getimage.php?imageid=$id' alt='$name'></a>";
}

?>

Still not working I feel that you are helping me get closer but still no cigar errors. Am I missing something?

agent_x

12:18 am on May 26, 2011 (gmt 0)

10+ Year Member



Well there are several things wrong here.

<?php
require_once('mysql_connect.php');

$id = $_GET["id"];

$query = mysql_query("SELECT id FROM imagetest WHERE id=$id") or die(mysql_error());

echo "$imageid";
?>


First off, in your html img tag you are passing a parameter called "imageid" to your image script, but in the image script you aren't using it, instead you are using one called "id" which won't be defined.

Second, in your query you are fetching the 'id' column but you already know the id, it's the 'image' column you want to fetch.

Next it doesn't seem as if you do anything with the result from the query, you just echo the variable $imageid which again isn't defined anywhere (you need to start the script with $imageid = $_GET["imageid"]; instead of the line you have).

Lastly you have forgotten to output the header for a jpg image first before echoing anything.

Fix those errors and things might start working.

To test that it is working, you should browse straight to the script itself in the browser, as if you were browsing direct to a jpg image - so if you go to

http://www.yourdomain.com/getimage.php?imageid=0


but of course the number on the end must be the id for an image that actually exists in your table - if it works you'll see your image, if not you may see something else or maybe nothing at all.

eta_carinae

9:57 pm on May 28, 2011 (gmt 0)

10+ Year Member



crazy88888888, why do you want to store images in database? the better practice is to store links to the images, it gives less head#*$!s