-1

I have an HTML tag <img> for specific products, however some products don't have images since their similar products already have the same. I want the viewer who reached these image-less products to be able to click on a link and get to the similar products. Now the <img> is like this:

<img src="../imgs/product/<?php echo $productName;?>.png" alt="No image for this item. Please refer to its similar item: <?php echo $alt; ?> "/>

Then I want the $alt part to be a link to the similar product, so I wrote:

<?php 
    $alt = '<a href=\"singleproduct.php?singleItem=' 
         . $similarProduct .'\">'. $similarProduct. '</a>'; 
?>

It's not possible to print out the link no matter how I revise the style of quotes. Just wondering if it's not possible at all to insert an <a> inside of <img>...? If not, how to make this work?

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Jasmin_W
  • 101
  • 6
  • you don't need to escape `\"` inside your single-quoted statement since you're using `$alt = '...';` as opposed to `$alt = "...";` - and why the jquery/css tags? – Funk Forty Niner Feb 04 '16 at 19:16
  • @Fred-ii- the `$alt` is going into that image, I think, where the double quotes are being used for the `alt` attribute in the HTML. Not sure if the `alt` can support links.. – chris85 Feb 04 '16 at 19:21
  • you're going about this the wrong way. What you're trying to do translates to `>` rather than `` It's hard to say exactly what you want to do here really. and no, the alt tag does not support href. – Funk Forty Niner Feb 04 '16 at 19:21
  • I don't think you can use the `alt` attribute to accomplish your goal. Here's a definition for the `alt` attribute *The alt attribute provides alternative information for an image if a user for some reason cannot view it (because of slow connection, an error in the src attribute, or if the user uses a screen reader).* – yardie Feb 04 '16 at 19:23
  • you can do `$alt = 'I am an alt tag';` though or `$alt = $similarProduct;` if you have a message assigned for that. If you want to add the href, you'll need to place it "after" your `` tag, it can't be part of the alt tag. Another thing, ternary operator. – Funk Forty Niner Feb 04 '16 at 19:25
  • you can't nest html like that. `alt=""` would probably work, because then you're not embedding "html", just htmll-encoded characters – Marc B Feb 04 '16 at 19:28
  • well you guys sort this out. @ me if you want me to submit my comments to an answer. I have to go take care of stuff ;-) – Funk Forty Niner Feb 04 '16 at 19:29
  • @chris85 This has been a *"talk amongst ourselves"* type of question. I had time to cut, split and pile a cord of wood already, and still nothing ;-) and no, alt does not support href. – Funk Forty Niner Feb 04 '16 at 20:06
  • I have no idea what this discussion is about. Why is everybody, including the OP, ignoring the _"some products don't have images"_ part of the question? I can't believe nobody pointed out yet that if there is no image, the solution is to not use an `` element. – Mr Lister Feb 13 '16 at 16:27

1 Answers1

0

You can't have a html tag in a html attibute. If you wan't a full html fallback, take a look at Inputting a default image in case the src attribute of an html <img> is not valid?

Community
  • 1
  • 1
Gwendal
  • 1,273
  • 7
  • 17
  • Indeed, sorry about that. I'm new to stackoverflow and didn't notice the show more comments button @Fred-ii- – Gwendal Feb 04 '16 at 19:28