crazy88888888 Posted May 13, 2011 Share Posted May 13, 2011 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>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/236337-need-help-with-php-a-href-and-img-src/ Share on other sites More sharing options...
requinix Posted May 13, 2011 Share Posted May 13, 2011 You're getting the binary... plus the HTML you wrapped around it. Remove the HTML. Quote Link to comment https://forums.phpfreaks.com/topic/236337-need-help-with-php-a-href-and-img-src/#findComment-1215092 Share on other sites More sharing options...
crazy88888888 Posted May 13, 2011 Author Share Posted May 13, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/236337-need-help-with-php-a-href-and-img-src/#findComment-1215098 Share on other sites More sharing options...
requinix Posted May 13, 2011 Share Posted May 13, 2011 You put the HTML in another script, which uses an to reference the one you posted. Quote Link to comment https://forums.phpfreaks.com/topic/236337-need-help-with-php-a-href-and-img-src/#findComment-1215112 Share on other sites More sharing options...
crazy88888888 Posted May 17, 2011 Author Share Posted May 17, 2011 I guess I don't quite understand why I need another script, Can you give me a code example of what you mean? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/236337-need-help-with-php-a-href-and-img-src/#findComment-1216583 Share on other sites More sharing options...
requinix Posted May 17, 2011 Share Posted May 17, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236337-need-help-with-php-a-href-and-img-src/#findComment-1216607 Share on other sites More sharing options...
crazy88888888 Posted May 17, 2011 Author Share Posted May 17, 2011 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>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/236337-need-help-with-php-a-href-and-img-src/#findComment-1216686 Share on other sites More sharing options...
requinix Posted May 17, 2011 Share Posted May 17, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236337-need-help-with-php-a-href-and-img-src/#findComment-1216696 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.