Reputation: 263
I am currently trying to create a shopping cart for my website and I have images of products stored in a database and I want to include them within <img src>
. By putting $get_row[imagesrc]
within the src. I need to know the correct way to add it to the below code as I dont fully understand the '
and .
tags
echo '<p>'.$get_row['name'].'<br/>'.$get_row['description'].'<br/>'.$get_row['imagesrc'].
'<br/>£'.number_format($get_row['price'],2).'<a href="cart.php?add='.$get_row['id'].'">Add</a></p>';
Upvotes: 3
Views: 172
Reputation: 516
. concatenates two strings, and ' is wrapped around a string.
so
echo 'Hello '.'World'; // Shows Hello World
I'd split yours up to make it easier to read:
echo '<p>';
echo $get_row['name'].'<br/>';
echo $get_row['description'].'<br/>';
echo '<img src="'.$get_row['imagesrc'].'" /><br/>';
echo '£'.number_format($get_row['price'],2);
echo '<a href="cart.php?add='.$get_row['id'].'">Add</a>';
echo '</p>';
But it all looks OK.
Upvotes: 2
Reputation: 7715
Check out this way of including PHP in your HTML. It's much easier to read and maintain. The last line in the paragraph is your image tag.
<p>
<?php echo $get_row['name']; ?><br/>
<?php echo $get_row['description']; ?><br/>
<?php echo $get_row['imagesrc']; ?><br/>
£<?php echo number_format($get_row['price'],2); ?>
<a href="cart.php?add=<?php echo $get_row['id']; ?>">Add</a>
<img src="<?php echo $get_row['imagesrc']; ?>" />
</p>
Upvotes: 1
Reputation: 72961
A specific answer has been given:
echo '<img src="'.$get_row['imagesrc'].'">';
Nonetheless, it's worth adding that you should:
You should escape output - with htmlspecialchars()
or otherwise.
echo '<img src="' . htmlspecialchars($get_row['imagesrc']) . '">';
Read the documentation on PHP Strings.
Upvotes: 1
Reputation: 6365
echo '<p>'.$get_row['name'].'<br/>'.$get_row['description'].'<br/><img src="'.$get_row['imagesrc'].'"><br/>£'.number_format($get_row['price'],2).'<a href="cart.php?add='.$get_row['id'].'">Add</a></p>';
Upvotes: 2
Reputation: 53198
This should achieve what you're looking for:
echo '<p>'.$get_row['name'].'<br/>'.$get_row['description'].'<br/><img src="'.$get_row['imagesrc'].'" /><br/>£'.number_format($get_row['price'],2).'<a href="cart.php?add='.$get_row['id'].'">Add</a></p>';
The '
character defines a string literal when it is wrapped around a series of characters.
The .
character is used for concatenating strings for output or storage.
Upvotes: 4
Reputation: 2841
echo '<p>'.$get_row['name'].'<br/>
<img src="'.$get_row['imagesrc'].'" alt="'.$get_row['name'].'"><br/>
<br/>£'.number_format($get_row['price'],2).'
<a href="cart.php?add='.$get_row['id'].'">Add</a></p>';`
Upvotes: 1