scmeeker Posted June 13, 2010 Share Posted June 13, 2010 I'm trying to make my text and images into clickable links. The images and text are in a database so its getting really tricky for me since I'm new to PHP. I've tried many different ways without success. Here is a snippet of the code I'm working with. As you can see, I'm also working with an image re-sizer. I would really like to make those images and text clickable links(i.e. Title). Any help is appreciated! if ($res) { while ($newArray = mysqli_fetch_array($res, MYSQLI_ASSOC)) { $detail = $newArray['id']; $photo = $newArray['photo']; $id = $newArray['title']; $price = $newArray['price']; list($width) = getimagesize($photo); // set the maximum width of the image here $maxWidth = 100; if ($width > $maxWidth) echo "<p><img alt=\"Image\" width=\"$maxWidth\" src=\"$photo\" />"; echo "Title:".$id." Price:".$price."<br/"; Quote Link to comment https://forums.phpfreaks.com/topic/204682-trying-to-make-text-and-images-into-clickable-links/ Share on other sites More sharing options...
Catfish Posted June 14, 2010 Share Posted June 14, 2010 Just add the link details (<a href></a>) into the output strings: echo "<p><a href=\"http://url.to.file/"><img alt=\"Image\" width=\"$maxWidth\" src=\"$photo\" />"; echo "Title:".$id."</a> Price:".$price."<br/"; Quote Link to comment https://forums.phpfreaks.com/topic/204682-trying-to-make-text-and-images-into-clickable-links/#findComment-1071637 Share on other sites More sharing options...
scmeeker Posted June 14, 2010 Author Share Posted June 14, 2010 For some reason it keeps giving me this error when I try to use it: Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' Here is the changed code: echo "<p><a href="painting.php"><img alt=\"Image\" width=\"$maxWidth\" src=\"$photo\" /></a>"; Quote Link to comment https://forums.phpfreaks.com/topic/204682-trying-to-make-text-and-images-into-clickable-links/#findComment-1071648 Share on other sites More sharing options...
Rustywolf Posted June 14, 2010 Share Posted June 14, 2010 For some reason it keeps giving me this error when I try to use it: Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' Here is the changed code: echo "<p><a href="painting.php"><img alt=\"Image\" width=\"$maxWidth\" src=\"$photo\" /></a>"; I believe it should be echo "<p><a href="painting.php"><img alt=\"$Image"\ width=\"$maxWidth"\ src=\"$photo"\ /></a>"; the second \ for each one was outside the ""'s and images was missing a $ Quote Link to comment https://forums.phpfreaks.com/topic/204682-trying-to-make-text-and-images-into-clickable-links/#findComment-1071658 Share on other sites More sharing options...
isedeasy Posted June 14, 2010 Share Posted June 14, 2010 For some reason it keeps giving me this error when I try to use it: Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' Here is the changed code: echo "<p><a href="painting.php"><img alt=\"Image\" width=\"$maxWidth\" src=\"$photo\" /></a>"; echo "<p><a href=\"painting.php\"><img alt=\"Image\" width=\"{$maxWidth}\" src=\"{$photo}\" /></a>"; Quote Link to comment https://forums.phpfreaks.com/topic/204682-trying-to-make-text-and-images-into-clickable-links/#findComment-1071663 Share on other sites More sharing options...
scmeeker Posted June 14, 2010 Author Share Posted June 14, 2010 Wow! Thanks everyone! It was isedeasy's post who resolved the problem. It was the brackets {} that I needed. This was the code that resolved my issue: echo "<p><a href=\"painting.php\"><img alt=\"Image\" width=\"{$maxWidth}\" src=\"{$photo}\" /></a>"; Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/204682-trying-to-make-text-and-images-into-clickable-links/#findComment-1071668 Share on other sites More sharing options...
isedeasy Posted June 14, 2010 Share Posted June 14, 2010 Actually it was the backslashes for the commas on the anchor tag that were needed. The curly brackets around the variables is just a habit of mine, they are not needed and to be honest I'm not sure if its good practise. Quote Link to comment https://forums.phpfreaks.com/topic/204682-trying-to-make-text-and-images-into-clickable-links/#findComment-1071672 Share on other sites More sharing options...
vijiravi Posted May 11, 2012 Share Posted May 11, 2012 I am trying to allow users to enter records that contain text and an image, then I want to fetch these records and make the image clickable link. How can this be done in PHP? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/204682-trying-to-make-text-and-images-into-clickable-links/#findComment-1344799 Share on other sites More sharing options...
QuickOldCar Posted May 12, 2012 Share Posted May 12, 2012 You should probably switch these around to make more sense. from: $detail = $newArray['id']; $photo = $newArray['photo']; $id = $newArray['title']; $price = $newArray['price']; to: $id = $newArray['id']; $photo = $newArray['photo']; $detail = $newArray['title']; $price = $newArray['price']; You should really do all the positioning with some sort of css styling to get it the exact way you want. echo "<p><a href=\"$photo\"><img title=\"$detail\" alt=\"$detail\" width=\"$maxWidth\" src=\"$photo\" /></a><br />$detail"; Other ways you can do the same links. If wrapped with double quotes, all single quotes within echo "<p><a href='$photo'><img title='$detail' alt='$detail' width='$maxWidth' src='$photo' /></a><br />$detail"; or use concatenation echo "<p><a href='".$photo."'><img title='".$detail."' alt='".$detail."' width='".$maxWidth."' src='".$photo."' /></a><br />".$detail; Quote Link to comment https://forums.phpfreaks.com/topic/204682-trying-to-make-text-and-images-into-clickable-links/#findComment-1344916 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.