Skip to content

Instantly share code, notes, and snippets.

@jonesch
Created October 5, 2012 15:55
Show Gist options
  • Save jonesch/3840665 to your computer and use it in GitHub Desktop.
Save jonesch/3840665 to your computer and use it in GitHub Desktop.
Loop through an array of menu items.
//Use CSS to make these columns responsive.
<ul class="food-menus<?=(' '.$menu_category)?>">
<?php
//Let's get a count of how many items we are going to display of our menu
$total_menu_categories = count($menu[$menu_category]);
$total_items = $total_menu_categories;
foreach($menu[$menu_category] as $section => $value) {
$total_items = $total_items + count($value);
} //get the total number of <li>s on our page
$number_of_columns = 3;
$ul_break_point = ceil($total_items/$number_of_columns);
//start the counting, and looping
$counter = 0;
foreach($menu[$menu_category] as $section => $value) {
if($section!='general'){ //general blurb for each category still counts as one item.
echo '<li class="title">'.str_replace('-', ' ', $section).'</li>'; $counter++;
}
foreach($value as $item => $details) { //each category within the menu, and all it's items
if($item=='overview'){ //overview of sub-category
echo '<li class="overview">'.$details.'</li>'; $counter++;
} else { //item title, information, price, description
echo '<li><p'.(empty($details['price'])?' class="none"':'').'>
<span class="title">'.$item.'</span>
<span class="price">'.$details['price'].'</span>
</p>'.(!empty($details['description'])?$details['description']:'&nbsp;').'</li>'; $counter++;
}
if($counter % $ul_break_point == 0){ //when do we need to break.
echo '</ul><ul class="food-menus '.$menu_category.'">';
}
}
}
?>
</ul><!-- .menus lunch-menu -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment