Jump to content

Need help with PHP a href and img src


Recommended Posts

Can someone give some advice why this %@!# code is not working. I keep getting the binary code when I don't have the header("Content-type: image/jpg"); in place but when I have the header I get nothing...calling on the PHP Gods to help. Here is what I have:

 

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

			   
$query = mysql_query("SELECT * FROM imagetest ORDER BY RAND()LIMIT 1");
$array = mysql_fetch_array($query);
$image = $array['image'];
$address = $array['address'];
$name = $array['name'];

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

Link to comment
Share on other sites

Thanks for your reply requinix. Taking out the HTML and just having echo $image; with the header works great, however, I want the user to be able to click the image and be directed to a url which I am trying to pull through my $address from my mysql How can I do this without HTML a href tag?

 

Link to comment
Share on other sites

It doesn't have to be another script per se, just a different chunk of code (it could be in the same file if you wanted).

With webpages you can't* have image data and other HTML at the same time. You need 1. the HTML that you want, containing an tag referring to 2. the image you want to display.

 

If you had all the PHP code in one file it could look like

// various includes or requires or whatever your script needs

if (isset($_GET["image"]) && ctype_digit($_GET["image"])) {
$imageid = $_GET["image"];

// find the image according to that $imageid
// send the appropriate Content-Type header() and output the image
exit; // so the rest of the script doesn't execute
}

// the script proceeds as "normal"

?>


There are two parts to the file: the first part shows the image and the second part shows the HTML.

 

* You can, but browser support is iffy.

Link to comment
Share on other sites

Look at this to see if this is what your talking about:

 

<?php

require_once('Connections/myConnection.php');

header("Content-type: image/jpg");

 

if (isset($_GET["image"]) && ctype_digit($_GET["image"])) {

$imageid = $_GET["image"];

 

$imageid = "SELECT * FROM `imagetest` ORDER BY RAND() LIMIT 1";

$query = mysql_query($imageid) or die(mysql_error());

 

while ($arrayImage = mysql_fetch_array($query, MYSQL_ASSOC)){

 

$imageid = $arrayImage['image'];

$address = $arrayImage['address'];

$name = $arrayImage['name'];

 

exit;

}

 

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

?>

Link to comment
Share on other sites

Hmm.

 

For now, use two separate files. It'll help you understand how the process works.

So make one file that outputs the HTML with the , and another file that looks up the ID and shows the image. Once that works, then you can think about combining the two into a single .php file.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.