0
$items = array(
'item1' => array('image' => 'img/image1.jpg', 'title' => 'Item1', 'price' => '1'),
'item2' => array('image' => 'img/image2.jpg', 'title' => 'Item2', 'price' => '2'),
);

foreach($items as $k => $v) {   
    $rows .= "<tr><td><img src='{$v['image']}'></td><td>{$v['title']}</td></tr>\n";
}

So I am expecting a table output but get into problems with the image. It does not display, I am guessing that it has to do with the quotation marks? Tried to change them in several ways like <img src='"'{$v['image']}'"'> but no luck. So what's the correct syntax?

1
  • Just check the source code of the page in browser. Your code should work, but keep in mind that you are linking to image relative to your current path not to absolute path of your website Commented Jan 3, 2014 at 13:15

5 Answers 5

2

Keep it simple,

foreach($items as $k => $v) {   
    $rows .= "<tr><td><img src='". $v['image']. "'></td><td>". $v['title']. "</td></tr>\n";
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, but still don't work. I do echo json_encode($_SESSION['item']); Maybe that's the problem?
Can you post your code in pastebin, so that we can have a look ?
Solved it, my problem was with the $_SESSION array and not the table. But you got me on the right track. Thanks!
2

Try this:

Pl confirm that path of images is correct.

<?php
$items = array(
'item1' => array('image' => 'img/image1.jpg', 'title' => 'Item1', 'price' => '1'),
'item2' => array('image' => 'img/image2.jpg', 'title' => 'Item2', 'price' => '2'),
);

foreach($items as $k => $v) {   
    $rows .= "<tr><td><img src='". $v['image']. "'></td><td>". $v['title']. "</td></tr>\n";
}
?>

Thanks!

Comments

1

you can try this:

foreach($items as $k => $v) {   
 $rows .= "<tr><td><img src='".$v['image']."'></td><td>".$v['title']."</td></tr>\n";
}

Comments

0

You may write your loop as follows:

foreach($items as $k => $v) {   
    $rows .= "<tr><td><img src='" . $v['image']. "'></td><td>" . $v['title'] . "</td></tr>\n";
}

Comments

0

You can write it in that way

$rows .= "<tr><td><img src='{$v[image]}'></td><td>{$v[title]}</td></tr>\n";

It will work too.

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.