Reputation: 171
I am trying to make a site which will echo all the images in a directory to the page but when I am echoing them it creates a separate div for each.
foreach($images as $img) {
echo "<div class=\"container\"><img class=\"photo\" src=\"{$img['file']}\" {$img['size'][3]} alt=\"\">\n</div>";
}
Upvotes: 0
Views: 781
Reputation: 789
Not sure why you doing everything in the PHP side, but avoid HTML in PHP as much as you can, due to it being dirty coding.
I would:
1. Set the images into a variable;
2. Send the variable to the html template;
3. Loop it in the template where html is predominant;
If you absolutely have to do HTML in PHP, then I would go for a sprintf approach;
echo '<div class="container">';
foreach ($images as $img) {
echo sprintf(
'<img src="%s" class="img-rounded" alt="%s">',
$img['file'],
$img['filename']
);
}
echo '</div>';
I find it clearer this way
Upvotes: 0
Reputation: 603
Please try this solution, maybe it can help to you:
echo "<div class='container'>";
foreach ($images as $img) {
echo "<img class='photo' src='{$img['file']}' {$img['size'][3]} alt=''>\n</div>";
}
echo "</div>";
Upvotes: 2
Reputation: 2420
You have to put div outside the foreach
echo "<div class=\"container\">";
foreach($images as $img) {
echo "<img class=\"photo\" src=\"{$img['file']}\" {$img['size'][3]} alt=\"\">";
}
echo "</div>";
With your actual code you create a <div>
every loop
Upvotes: 1
Reputation:
Place the <div>
around your foreach()
loop:
echo '<div class="container">';
foreach($images as $img) {
echo "<img class=\"photo\" src=\"{$img['file']}\" {$img['size'][3]} alt=\"\">";
}
echo '</div>';
Upvotes: 3