3

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/>&pound;'.number_format($get_row['price'],2).'<a href="cart.php?add='.$get_row['id'].'">Add</a></p>';
1
  • You will really get mad in generating html this way - get into some templating engine like Smarty or Twig. Commented Jan 7, 2013 at 17:15

7 Answers 7

4

This should achieve what you're looking for:

echo '<p>'.$get_row['name'].'<br/>'.$get_row['description'].'<br/><img src="'.$get_row['imagesrc'].'" /><br/>&pound;'.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.

Sign up to request clarification or add additional context in comments.

Comments

2
echo '<p>'.$get_row['name'].'<br/>'.$get_row['description'].'<br/><img src="'.$get_row['imagesrc'].'"><br/>&pound;'.number_format($get_row['price'],2).'<a href="cart.php?add='.$get_row['id'].'">Add</a></p>';

Comments

2

. 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 '&pound;'.number_format($get_row['price'],2);
    echo '<a href="cart.php?add='.$get_row['id'].'">Add</a>';
echo '</p>';

But it all looks OK.

Comments

1
echo '<p>'.$get_row['name'].'<br/>
<img src="'.$get_row['imagesrc'].'" alt="'.$get_row['name'].'"><br/>
<br/>&pound;'.number_format($get_row['price'],2).'
<a href="cart.php?add='.$get_row['id'].'">Add</a></p>';`

Comments

1
echo '<img src="'.$get_row['imagesrc'].'">';

Try that.

Comments

1

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.

Comments

1

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/>
    &pound;<?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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.