phparrayscodeignitermodel-view-controllerresultset

How to convert a CodeIgniter result set object into an array of arrays?


I am new to CodeIgniter. In the model, I have the following code:

public function get_all_subjects()
{
   return $this->db->get('subjects');
}

In the controller, I have:

public function index()
{
   $this->load->model('subjects');
   $data['content'] = $this->subjects->get_all_subjects();

   $this->load->view('home', $data);
}

I am trying to get the values in the view:

foreach($content as $val)
{
   echo $val['subject']; //i am getting error, Message: Undefined index: subject
}

The fields in the subjects table are subject_id and subject.

I am getting this error message:

Undefined index: subject


Solution

  • public function get_all_subjects()
        {
            return $this->db->get('subjects')->result_array();
        }
    

    You are not returning result from your query. You have just run the query.