Search the Community
Showing results for tags 'loops'.
-
Multiple lists and loops into dynamic reusable piece of code
arjen posted a topic in General Support
$jurisdictionsPageChildren = $pages->get(1011)->children(); // Check whether there are any children before attempting to split them if($jurisdictionsPageChildren > 0) { // Some general vars $rows = 2; $totalCount = count($jurisdictionsPageChildren); $rowCount = $totalCount / $rows; // Creating the seperate list (there has to be an easier way to do this) $firstList = $jurisdictionsPageChildren->slice(0, $rowCount); $secondList = $jurisdictionsPageChildren->slice($rowCount, $totalCount) $mainContent .= "<div class='row jurisdictions-list'>"; // First list of elements $mainContent .= "<div class='columns six alpha'>"; foreach($firstList as $child) { $mainContent .= "<h2><a$class href='{$child->url}' title='{$child->title}'>{$child->title}</a></h2>"; } $mainContent .= "</div>"; // Second list of elements $mainContent .= "<div class='columns six omega'>"; foreach($secondList as $child) { $mainContent .= "<h2><a$class href='{$child->url}' title='{$child->title}'>{$child->title}</a></h2>"; } $mainContent .= "</div>"; $mainContent .= "</div>"; // Close the row class } Okay, here is the deal. I have one list ($jurisdictionsPageChildren) which I want to split into multiple blocks/columns. I would like to adjust this code so I can set the $rows variable to some number. The condition is that the first block (regardless of the number of blocks) has to have a class 'alpha' and that every last block has to have a class 'omega'. I was thinking to put a foreach in a foreach, but somewhere things got messed up. If this was JSP it would do it like this: <c:set var="splittedListHere" value="$(rpfn:splitListFunction(listHere, 2))" /> <c:forEach items="splittedListHere" var="list" varStatus="i"> <div class="block$(i.first ? ' alpha' :'')$(i.last ? ' omega')"> more foreach stuff </div> </c:forEach> Any thoughts to make this prettier for a PHP noob are very welcome.