A PHP Error was encountered Message: Undefined variable: menu - CodeIgniter - php

<?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);
}
}
Hi guys, I get menu details at the constructor like this.I can get the result also. but it comes with the error
"A PHP Error was encountered
Severity: Notice
Message: Undefined variable: menu
"

You need to remove the dollar sign before menu, because you're referencing local variables
$this->menu = $this->menu_model->generateTree($items=array());

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);
}
}

Use $menu as below in your file, hope this solution help you.
$this->menu

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);
}

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);
}
}

Related

function return to a variable in utility.php in codeigniter

I am new in codeigniter.
I need to do the following code in utility.php which is a custom library.
class Utility{
public $x=test("123");
public function test($name)
{
return $name;
}
echo $x;
}
Thanks in advance
This is showing syntax error.
Maybe this way?
class Utility
{
public $x;
public function __construct($name = 'default name')
{
$this->x = $this->test($name);
}
public function test($name)
{
return $name;
}
}
Then in controller you would go with
class Welcome extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index($name = null)
{
$this->load->library('utility', [$name]);
echo $this->utility->x;
}
}

php codeigniter single function for all other function

here is some details for my question.
controller:
function get_info()
{
$user_id = $this->session->userdata['logged_in']['userid']
$this->load->model('message_model');
$msg_data['msg']=$this->message_model->get_user_msg($user_id);
$this->load->view('header',$msg_data);
$this->load->view('content');
$this->load->view('footer');
}
function others_goes_here()
{
$user_id = $this->session->userdata['logged_in']['userid']
$this->load->model('message_model');
$msg_data['msg']=$this->message_model->get_user_msg($user_id);
LOAD OTHER VIEWS.....
}
model:
function get_user_msg($user_id)
{
QUERY GOES HERE......
}
Now what i want to achieve is how can i make the get_user_msg to be called only once without calling it to all my other functions. Hope you guys can help me with this,
thank in advance.
You create a class variable and run get_user_msg from the constructor:
protected $msg_data;
function __construct() {
parent::__construct();
$this->load->model('message_model');
$user_id = $this->session->userdata['logged_in']['userid']
$this->msg_data = $this->message_model->get_user_msg($user_id);
}
function function get_info() {
$this->load->view('my_view', $this->msg_data);
}
Well use construct function for that
public $user_msg;
function __construct()
{
parent::__construct();
$this->load->model('message_model');
$user_id = $this->session->userdata['logged_in']['userid']
$this->user_msg=$this->message_model->get_user_msg($user_id);
}
function AllOtherFunctions(){
$this->user_msg; //you will have it ready every where
}

Undefined property when extending CI_Model in CodeIgniter [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Codeigniter Message: Undefined property: stdClass
location: application/core/student_model.php
class Student_model extends CI_Model
{
private $id;
public function __construct() {
parent::__construct();
}
public function setId($id){
$this->id = $id;
}
public function getId() {
return $this->id;
}
}
location: application/controllers/test.php
class Test extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('Student_model');
}
public function index()
{
$student = $this->student_model->setId('1234567');
echo $student->getId();
}
}
I am getting the following message/error.
Message: Undefined property: Test::$student_model
Filename: controllers/test.php
Line Number: 13
This line is where I am calling the method setId.
Can anyone see what I am doing wrong?
Try replacing
$this->student_model->setId('1234567');
with
$this->Student_model->setId('1234567');
Your class is capitalized, so the property should also be capitalized afaik.
Try
public function __construct()
{
parent::__construct();
$this->load->model('Student_model', 's_model');
}
public function index()
{
$student = $this->s_model->setId('1234567');
echo $student->getId();
}
what you are doing wrong is :
you are assigning $student to $this->student_model->setId('1234567');
which is a function or 'setter' that does not return anything; and you won't be able to use $student->getId();
what I would do is that
class Test extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('student_model', 'student');
}
public function index()
{
$this->student->setId('1234567');
echo $this->student->getId();
}
}

Why can I not pass values initialized in the constructor of my Code Igniter controller?

I have this controller in Code Igniter application. A value is initialized in the constructor.
class Cat extends CI_Controller {
private $data = array();
public function __construct() {
parent::__construct();
$this->data['sound'] = "meow";
}
public function index() {
$this->load->view('myCatPage', $data);
}
}
The view "views/myCatPage.php" looks like this. It is simple.
<?= $sound ?>
Why does PHP note this error?
Message: Undefined variable: sound
I thought I sent this variable as a key in the array ($data) I sent into the view.
I have tried
$this->load->view('myCatPage', $this->data);
but that strangely fails too.
class Cat extends CI_Controller {
var $data = array();
public function __construct() {
parent::__construct();
$this->data['sound'] = "meow";
}
public function index() {
$this->load->view('myCatPage', $this->data);
}
}

How to run method inside a Model | MVC

In the index file i have _autoload and load the libs and then i explode the url to get the wanted contoller and the model if exists. In the view i can see the model __construct() so the model is loaded but if i try to use $this->model->test(); i get
Call to a member function test() on a non-object
http://site.com/about
$this->request = about;
$controller = new $this->request;
$controller->loadModel($this->request);
Everething work ok
*Here is the Main controller *
class Conroller {
function __construct() {
// echo 'Main controller<br />';
$this->view = new View();
}
public function loadModel($name) {
$path = 'models/'.$name.'_model.php';
if (file_exists($path)) {
require 'models/'.$name.'_model.php';
$modelName = $name . '_model';
// **here i make the object**
$this->model = new $modelName();
}
}
}
Here is the About model
class about_model{
function __construct() {
echo 'test';
}
public function test() {
$test = 'test one';
}
}
Here is the About Conroller
class About extends Conroller {
function __construct(){
parent::__construct();
$this->model->test();
$this->view->render('/about');
}
}
You will need to call loadModel in your About controller before you refer to the model:
class About extends Conroller {
function __construct(){
parent::__construct();
$this->loadModel('about');
$this->about->test();
}
}

Categories