Will Barangi Will Barangi - 2 months ago 5
HTML Question

display image from a PHP for loop

So I have an array that contains information about items..I have also an image folder with the name of the files coinciding with one of the array values specifically $arr[$key]['isbn'];

I have made a for loop to go through the array and display the images along with some information like item price.The problem is that in the image tag I am trying to include< img src="$arr[$key]['isbn'].jpg and it's not working
here is my code

for($row = 0; $row < 6;$row++){
echo '<img src="$arr[$row]['isbn'].jpg" alt="Mountain View" style="width:304px;height:228px;">'.$arr[$row]['title'].'<br />'.'by '.$arr[$row]['author']. '<br /> '.'<input type="radio" name="booktype" value="hardcover" >Hardcover: '.'$'.$arr[$row]['hardcover']. "<br /> ".'<input type="radio" name="booktype" value="softcover" >Softcover: '. "$".$arr[$row]['softcover']. "<br /> ".'<input type="radio" name="booktype" value="e-book" >E-Book: '."$".$arr[$row]['e-book']. " ".'<br />';
}


when I print it out this is what I get
enter image description here

Here is my question first how do I refer to the isbn value and append .jpg to get image.
2. Notice how the image is displaying and then other images are below it..I would like it to be inline; line have an image, then below it the prices and then besides it the next image...
thank you please help

Answer

Your code is wrong, try the following:

for($row = 0; $row < 6;$row++){
     echo '<img src="'.$arr[$row]['isbn'].'.jpg" alt="Mountain View" style="width:304px;height:228px;">'.$arr[$row]['title'].'<br/>by '.$arr[$row]['author'].'<br/><input type="radio" name="booktype" value="hardcover" >Hardcover: $'.$arr[$row]['hardcover'].'<br/><input type="radio" name="booktype" value="softcover" >Softcover: $'.$arr[$row]['softcover'].'<br/><input type="radio" name="booktype" value="e-book" >E-Book: $'.$arr[$row]['e-book'].'<br/>';
}

You can use variables only on string with " not with '. See the name of the image. There you are using the variable $arr[$row]['isbn'] on a string surrounded with '.

You can find a very good explanation about the difference between " and ' on PHP here: http://stackoverflow.com/a/3446286/3840840

Comments