[eluser]Unknown[/eluser]
I am trying to make a restaurant menu and I have to have **Categories** and **Subcategories**, so I created the following tables:
Table: Categories
ID
NAME
Table: Subcategories
ID
NAME
PRICE
DESCRIPTION
CATEGORIES_ID
I am using DataMapper and the problem I having with the code below is that the result of the FOREACH LOOPS I am getting is that only the LAST Subcategory is showing, where there are few more entries.
So I have 2 entries each in the tables for SubCategories and the result of the code is giving me this:
Quote:ALCOHOL -> VODKA
NONALCOHOL -> FANTA
and it needs to be
Quote:ALCOHOL -> VODKA, AFTERSHOCK,
NONALCOHOL -> FANTA, SPRITE
Code:
public function index()
{
$categories = new Category_model();
$subcategories = new Subcategory_model();
$categories->get();
$subcategories->get();
$recent_categories = array();
foreach ($categories as $category)
{
foreach($subcategories as $subcategory)
{
if($subcategory->categories_id == $category->id)
{
$single_category = array
(
'category_name' => $category->name,
'subcategory_name' => $subcategory->name,
'subcategory_description' => $subcategory->description,
'subcategory_price' => $subcategory->price,
);
}
}
array_push($recent_categories, $single_category);
}
$data = array
(
'categories' => $recent_categories,
);
$this->parser->parse('templates/content_menu', $data);
}
THIS IS THE VIEW:
Code:
{categories}
<div class="grid-12 ourmenu">
<!-- MENU TITLE -->
<h2>{category_name}</h2>
<hr/>
<!-- MENU ITEM -->
<h4>
<a class="clb-photo" href="<?php echo site_url('design/images/photos/food.jpg'); ?>">
{subcategory_name}
</a>
<span>{subcategory_price}</span>
</h4>
<p>{subcategory_description}</p>
</div>
{/categories}