A PHP Error was encountered Message: Undefined variable: menu - CodeIgniter
Get the solution ↓↓↓Solution:
You need to remove the dollar sign before menu, because you're referencing local variables
$this->menu = $this->menu_model->generateTree($items=array());
Answer
Solution:
class Dashboard extends CI_Controller {
private $menu;
public function __construct() {
parent::__construct();
$this->load->model('menu_model');
$this->menu = $this->menu_model->generateTree($items = array());
}
public function index() {
$data['menu'] = $this->menu;
$this->load->view('dashboard/dashboard', $data);
}
}
Answer
Solution:
Use$menu
as below in your file, hope this solution help you.
$this->menu
Answer
Solution:
Remove$
from$this->$menu
public function __construct()
{
parent::__construct();
$this->load->model('menu_model');
$this->menu = $this->menu_model->generateTree($items=array());
}
public function index()
{
$data['menu'] = $this->menu;
$this->load->view('dashboard/dashboard', $data);
}
Answer
Solution:
while accessing the class variable you need not to use $ for second time after $this so it would be like below code
for example
$this->variable_name
your code would be as follows
<?php
class Dashboard extends CI_Controller {
private $menu;
public function __construct() {
parent::__construct();
$this->load->model('menu_model');
$this->menu = $this->menu_model->generateTree($items = array());
}
public function index() {
$data['menu'] = $this->menu;
$this->load->view('dashboard/dashboard', $data);
}
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: undefined array key
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.