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

[eluser]Unknown[/eluser]
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:
<?php
class Menu extends CI_Controller {

public function __construct()
{
parent::__construct();
$this->load->model('menu_model');
}

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);
$this->load->view('templates/footer');
}
}

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

public function __construct()
{
$this->load->database();
}

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
<ul>
&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
?&gt;</li>
&lt;?php echo br();?&gt;
&lt;?php foreach($sub_menu as $sub_menu_item):
echo nbs(4);?&gt;
<li>&lt;?php
echo $menu_item['id']; //I get - Message: Undefined variable: id
echo $sub_menu_item['title'];?&gt;</li>
&lt;?php echo br();
endforeach;
echo br(2);
endforeach;
?&gt;
</ul>


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...




Theme © iAndrew 2016 - Forum software by © MyBB