1

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

0

1 Answer 1

7
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.

Sign up to request clarification or add additional context in comments.

2 Comments

return $this->db->get('subjects')->result_array(); should do the trick and would avoid you from creating unnecessary variables: $q. Maybe @DeiForm wants to update his function.
Yes I heard of this chaining method. But for beginers the step by step method is easily to understand

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.