Roderick Posted December 14, 2015 Share Posted December 14, 2015 I'm trying to add a class and <div> tags to part of an array. It's an array of different fields (images and text) from a number of child pages. I want the result of each child to be styled individually, but so far, I've only been able to style the whole array or each individual item. I tried: <div class="my-class"> <?php foreach($page->children as $child): foreach($child->images as $image): echo "<img src='$image->url'/>"; endforeach; endforeach; ?> </div> which adds a class to the whole array. Or: <?php foreach($page->children as $child): foreach($child->images as $image): echo "<div class='my-class'> <img src='$image->url'/></div>"; endforeach; endforeach; ?> which adds a class to each individual field. I understand that I have to add something to the code that divides the array for each child page, but how? Link to comment Share on other sites More sharing options...
AndZyk Posted December 14, 2015 Share Posted December 14, 2015 Hello Roderick, are you searching for this solution? <?php foreach($page->children as $child): ?> <div class="my-class"> <?php foreach($child->images as $image): echo "<img src='$image->url'/>"; endforeach; ?> </div> <?php endforeach; ?> Regards, Andreas Link to comment Share on other sites More sharing options...
pwired Posted December 15, 2015 Share Posted December 15, 2015 wouldn't this code do the same ? <?php foreach($page->children as $child): <div class="my-class"> foreach($child->images as $image): echo "<img src='$image->url'/>"; endforeach; </div> endforeach; ?> Link to comment Share on other sites More sharing options...
diogo Posted December 15, 2015 Share Posted December 15, 2015 wouldn't this code do the same ? Try it out and let the error message tell you 2 Link to comment Share on other sites More sharing options...
Tom. Posted December 15, 2015 Share Posted December 15, 2015 wouldn't this code do the same ? <?php foreach($page->children as $child): <div class="my-class"> foreach($child->images as $image): echo "<img src='$image->url'/>"; endforeach; </div> endforeach; ?> <?php foreach($page->children as $child) { echo "<div class='my-class'>"; foreach($child->images as $image) { echo "<img src='$image->url'/>"; } echo "</div>"; } ?> Link to comment Share on other sites More sharing options...
Roderick Posted December 15, 2015 Author Share Posted December 15, 2015 Tom's suggestion works, thanks. It's a bit confusing to me to see people using either curly brackets or a colon when calling foreach, but I guess it's up to the user to choose their preferred method. Link to comment Share on other sites More sharing options...
AndZyk Posted December 15, 2015 Share Posted December 15, 2015 My suggestion does the same as Tom's, but I forgot, that you would like to echo the HTML code. Personally, I don't like to echo every bit of HTML, but as you said, it is a matter of preference. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now