Welcome Guest, Not a member yet? Register   Sign In
Confused about use of $data between Model, Controller and View
#1

[eluser]MightyBishop[/eluser]
Hi

Only just discovered Code Igniter and wishing I had discovered it a LONG time ago!

I've come across something I just can't get my head round.

I have a call to the database in my model code:

Code:
public function get_all_schools()
{
  $query = $this->db->query('SELECT * FROM SCHOOLS WHERE DELETED = 0');
  return $query->result_array();
}

I use this in my Controller code with a call to a view:


Code:
$data['schools'] = $this->aspirations_model->get_all_schools();
  $this->load->view('aspirations/create',$data);


And then in my view code I create a select control in HTML using $data:


Code:
<select class="mystyles" name="myschool">
  &lt;?php foreach ($schools as $school_item): ?&gt;
  <option value="&lt;?php echo($school_item['ID_SCHOOL']); ?&gt;">&lt;?php echo($school_item['NAME_OF_SCHOOL']); ?&gt;</option>
  &lt;?php endforeach; ?&gt;
  </select>


This all works perfectly - the Select box shows the list of schools. SPLENDID. Just as expected as per my understanding of PHP and CodeIgniter.

Here's the problem. I have another table in my database - DESTINATIONS. I do EXACTLY the same thing with THAT table (and pretty much any other table), and I try and use it and it does not work.

What happens is that the data is correctly pulled out in the Model, but somehow is not getting into the view:

All I've added is this code in the Model. This works, and pulls out the correct data. I've checked with a little test code inside the call.


Code:
public function get_all_destinations()
{
  $query = $this->db->query('SELECT * FROM DESTINATIONS WHERE DELETED = 0');
  return $query->row_array();
}


And this is essentially the code in the Controller:


Code:
$data['schools'] = $this->aspirations_model->get_all_schools();
  $data['destinations'] = $this->aspirations_model->get_all_destinations();
    
  $this->load->view('aspirations/create',$data);


And this is the code in the view:
Code:
<select class="mystyles" name="mydestination">
  &lt;?php foreach ($destinations as $dest_item): ?&gt;
  <option value="&lt;?php echo($dest_item['ID_DESTINATION']); ?&gt;">&lt;?php echo($dest_item['DESTINATION']); ?&gt;</option>
  &lt;?php endforeach; ?&gt;
  </select>


But it just loads up with completely weird results! I've checked the database schema - the two tables are identical.

I've wracked by brains over this, but my knowledge is too limited to get any further.

Why does the first call work perfectly, but the second one doesn't? I can only assume I don't fully understand the use of the $data variable.

Am I asking too much of it? If so, how do you pass multiple pieces of data from a Model through a Controller to a View?

Can anyone tell me where I am going wrong here? Total noob to this, but I REALLY want to get to grips with this, because I think this Code Igniter stuff rocks and I want to crack it!

Thanks for any help.

Yours

JMB


Messages In This Thread
Confused about use of $data between Model, Controller and View - by El Forum - 09-07-2012, 01:58 AM



Theme © iAndrew 2016 - Forum software by © MyBB