views:

3176

answers:

4
<a href="'.$productLink.'" alt="'.$productName.'">
<img src="'.$productImg1URL.'" alt="'.$productName.' '.$productType.' ">
</a>

Hello the img src is actually in a different directory /images

I know this is probably super easy but Ive spent an hour on it and nothing. The page works but doesn't show the directory. Please help the rookie. I took out the < in front of the a href and img src couldn't make the page display

+6  A: 

There are two ways of doing this:

Firstly, outside of a code block:

<?php
// code here
?>
<a href="<?= $productLink ?>" alt="<?= $productName ?>">
<!-- or -->
<img src="<?echo $productImgURL; ?> alt="<?php echo $productName . ' ' . $productType ?>">
</a>

The first form is called the short open tag and has to be enabled in your php.ini which it almost always is.

Or if you're doing this inside a code block:

<?php
echo <<<END
<a href="$productLink" alt="$productName">
<img src="$productImgLURL" alt="$productName $productType">
</a>
END

The <<<END is a heredoc and I usually prefer it to using a big string within double quotes, which requires escaping contained double quotes.

cletus
+1  A: 

Job #1 when you have a problem with code that is generating HTML, is look at the output source and compare it with what you expect. I've had problems like this before and they usually vanished when I stopped thinking about the PHP code, and looked at the actual output.

What is the content of $productImg1URL - and if the images that it's referencing are in the URL starting /images/ - does that start $productImg1URL. If it's just the name of an image, but without the path - you have to put that into place.

Alister Bulman
A: 

Great post....this saved hours of work for me ,,,

Mark Scott
A: 

thanks..Good post

diya