Zack Boniphace
Zack Boniphace

Reputation: 117

Undefined variable in the template

I am getting the undefined variable whenever I load the controller.

Here is the index function from the controller that gets called ( 4th line ) and the same error in every other occurrence.

<?php

class Students extends CI_Controller {

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

public function index() {
    $data['view_name'] = 'students/dashboard';
    $this->load->view('templates/template_student' , $data );
}

}

And the part of the template where the error comes from This is a part of the code from the template_student template

<ul class="nav navbar-nav navbar-right">
<li class="">
<a href="javascript:;" class="user-profile dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
<?php if ($student->passport_photo == '') { ?>
<img src="<?php echo base_url(); ?>assets/images/sample.jpg" alt="">
<?php } else { ?>
<img src="<?php echo base_url(); ?><?php echo $student->passport_photo; ?>" alt="">
<?php } ?>
<?php if ($this->session->userdata('logged_in')) : ?>
<?php echo $this->session->userdata('first_name') . " " . $this->session->userdata('last_name'); ?>
<?php endif; ?>
<span class=" fa fa-angle-down"></span>
</a>
<ul class="dropdown-menu dropdown-usermenu pull-right">
<li><a href="<?php echo base_url(); ?>students/profile">  Profile</a>
</li>
<li><a href="<?php echo base_url(); ?>students/logout"><i class="fa fa-sign-out pull-right"></i> Log Out</a>
</li>
</ul>
</li>
</ul>

What have I done wrong?

Upvotes: 1

Views: 1584

Answers (2)

Abdul Salam
Abdul Salam

Reputation: 406

Changes in controller

<?php

class Students extends CI_Controller {

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

public function index() {
    $data = array();
    $data['view_name'] = 'students/dashboard';
    $data['student'] = $this->student_model->get_all();
    $this->load->view('templates/template_student' , $data );
}

}

Then you have to add a get_all() function in your Student_model class as follows

function get_all()
        {           
            $this->db->from($this->table);

            $query=$this->db->get();
            if($query->num_rows()>0){
                    return $query->result_array();
                }
                else{
                    return array();
                }
        }

here $this->table should be the students table name

Now i hope you can check the value passport_photo is exist or not in your views something similar to

<?php if($student[0]['passport_photo'] == ''){
    .
    .
    .

    } ?>

I hope this may helps you..thanks!

Upvotes: 0

&#244;kio
&#244;kio

Reputation: 1790

You have loaded a model called Student in your controller, and in your template your are accessing a $student variable. That's 2 different things.

You should do something like this :

class Students extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('Student_model', 'student');
    }

    public function index() 
    {
        $student = ... // retrieve the student from db
        $data['student'] = $student; // define the variable to be sent to the view
        $this->load->view('templates/template_student' , $data );
    }
}

Upvotes: 1

Related Questions