user2402963 user2402963 - 1 month ago 6
PHP Question

php array with image in table

$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?

Answer

Keep it simple,

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