Reputation: 477
I would like to have the following json structure. I have generate the half part but don't know how to put contents into menu item which is not a top menu.
my tables structure:
and here is my controller code for generating the menu
public function index()
{
$data['menus'] = $this->main_menu_model->get_all_menus3();
$result=$this->build_menu($data['menus'],0);
echo '<pre>'; print_r($result); die;
}
function build_menu($lists,$parent_id)
{
// return an array of items with parent = $parentId
$result = array();
foreach ($lists as $item)
{
if ($item['parent'] == $parent_id)
{
$newItem = $item;
$newItem['contents'] = $this->build_menu($lists, $newItem['id']);
$result[] = $newItem;
}
}
if (count($result) > 0) return $result;
return null;
}
its give me this result :
[0] => Array
(
[id] => 1
[title] => About Sotogrande
[bg_image] => bg-4.jpg
[position] => 1
[parent] => 0
[tbl_name] =>
[contents] => Array
(
[0] => Array
(
[id] => 2
[title] => History
[bg_image] => bg-92.jpg
[position] => 1
[parent] => 1
[tbl_name] => history
[contents] =>
)
[1] => Array
(
[id] => 3
[title] => Photos
[bg_image] => bg-8.jpg
[position] => 2
[parent] => 1
[tbl_name] => photos
[contents] =>
)
[2] => Array
(
[id] => 4
[title] => Activities
[bg_image] => images3.jpg
[position] => 3
[parent] => 1
[tbl_name] => activities
[contents] =>
)
)
I want to put the actual contents get from table into the contents= [] where its a sub menu not for top menu[parent]. I have table for every menu item. but in some sub menu i have type field and if its a type== gallery then i have a gallery table so the contents= [result of gallery table].
Upvotes: 0
Views: 581
Reputation: 337
not sure if this question is related to html/css to display results or the issue is passing data from controller to a view or the problem is sql statement elaborate more
Upvotes: 1