Welcome Guest, Not a member yet? Register   Sign In
Menu test problem

Hi Im new to CI and trying to learn and I have read the entire User guide. Smile
I know some php but Im absolutely no pro...

I have used the setup from the news tutorial in the User guide.

My 3 files are:

1. My controller - controllers/menu.php:
class Menu extends CI_Controller {

public function __construct()

public function index()
$data['page_title'] = "Menu tester title";
$data['heading'] = "This is my menu";

$data['menu'] = $this->menu_model->get_menu();
$data['sub_menu'] = $this->menu_model->get_sub_menu();

$this->load->view('templates/header', $data);
$this->load->view('menu/index', $data);

2. My model - models/menu_model.php:
class Menu_model extends CI_Model{

public function __construct()

public function get_menu()
$query = $this->db->get_where('content', array('parent_id' => 0));
return $query->result_array();

public function get_sub_menu()
//$id = $menu_item['id'];
$query = $this->db->get_where('content', array('id' => $id));
return $query->result_array();


3. My view views/menu/index.php
&lt;?php foreach($menu as $menu_item): ?&gt;

<li>&lt;?php echo $menu_item['title'];
echo br();
echo 'id:'.nbs(). $menu_item['id'];
$id = $menu_item['id']; //works fine here
&lt;?php echo br();?&gt;
&lt;?php foreach($sub_menu as $sub_menu_item):
echo nbs(4);?&gt;
echo $menu_item['id']; //I get - Message: Undefined variable: id
echo $sub_menu_item['title'];?&gt;</li>
&lt;?php echo br();
echo br(2);

My view propably isn't the prettiest, but hey im learning Wink

As you can see in my notes in my views/menu/index.php, the $id variable ins't defined... so Im losing the 'id' I need to make the second foreach Sad

Im not sure if it is at all possible to make two foreach loops inside each other like this...

How to fix my problem?

Is it a totally insane way to make a menu?

Any pointers? pls...

Messages In This Thread
Menu test problem - by El Forum - 01-06-2012, 05:20 AM

Theme © iAndrew 2016 - Forum software by © MyBB