Codeigniter视图未定义变量[关闭]

I'm getting the following error: Undefined variable: section in my home_view controller. I've included line 17 in home_view.php and I'm pretty sure I'm passing all variables in correctly. Why is it giving me this error?


  <?php

    class Student extends CI_Controller  {


    function index()
          {
            $this->load->model('student_model');
            $data['section']= $this->student_model->browse_section();
            $this->load->view('home_view',$data);
          }

       }

student_model.php

<?php

Class Student_Model extends CI_Model 
{

function browse_section() 
    {

    $q = $this->db->get('section');

        if($q->num_rows > 0 )
        {
        foreach ($q ->result() as $row )
                {
                $data[] = $row ;
                }
        }

        return $data;
    }

}

Home_view.php

 <?php foreach($section as $r){
    echo '<h1>' . $r->Time . '</h1>' ; 
}
?>

your $data variable is undefined. You need to define it before the if function in the model :

function browse_section() 
{

$q = $this->db->get('section');
$data = array(); //define the data array.

    if($q->num_rows > 0 )
    {
    foreach ($q ->result() as $row )
            {
            $data[] = $row ;
            }
    }

    return $data;
}

hope this helps

  • That foreach loop in your model is not needed. Just return $q->result().
  • num_rows is a method so your code should look like: $q->num_rows() instead of $q->num_rows
  • Try to enable php display errors on your development environment which will help you fix these kind of bugs in a matter of minutes.

I hope these points help you further.

The problem is this line:

$data['section']= $this->student_model->browse_section();

Not only you do not have $data defined, but also you do not have the key section defined. Please take a look at the documentation of arrays:

Note:

Attempting to access an array key which has not been defined is the same as accessing any other undefined variable: an E_NOTICE-level error message will be issued, and the result will be NULL.

I am not sure, what you are trying to achieve, but this may be enough to fix your code in Student controller:

$this->load->model('student_model');
$data = array();
$data['section']= $this->student_model->browse_section();
$this->load->view('home_view',$data);

To fix the part in the model, follow KDaker's tips.

Also read about what is undefined variable in PHP, and probably familiarize yourself with object-oriented programming. That will probably help you avoid similar and more important mistakes in the future.

let me explain to you the error you are getting and correct it later on

lets start with the query on the model

<?php

Class Student_Model extends CI_Model 
{

function browse_section() 
    {

    $q = $this->db->get('section');

        if($q->num_rows > 0 )
        {
        foreach ($q ->result() as $row )
                {
                $data[] = $row ;
                }
        }

        return $data;
    }

}

clearly if the result set from the database is EMPTY then $data wouldn't be supplied with any value and the function browse_section() with the return $data; would be NULL in return to the controller and if passed to the view it will 100% create an Error because there's nothing to loop.

here are some suggestions to correct the issue 1.) before assigning a return to a function make sure it has default value so assigning this line $data = array(); on the first line in browse_section model function will do. this will make sure that you return an empty Array that can be passed to the controller then on the view if no value is returned. 2.) I suggest you use result_array() for extracting Database records w/c has better use for array manipulation.


About the Controller nothing needs modifications.

__ About the view if you decided to use result_array() on the model you can change the foreach to but this is optional

 <?php foreach($section as $key => $value){
    echo '<h1>' . $value['Time'] . '</h1>' ; 
}
?>

but make sure to put

if(!is_null($section) && count($section) > 0) { 
//foreach here
}

this line will surely save you!

Happy Coding My friend!