• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
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

#2
[eluser]Aken[/eluser]
It's very hard to diagnose a problem without seeing the actual code and the actual error / result. Don't paraphrase a problem or your code, show us exactly what you have.

Your example code looks okay. Maybe check that your destinations DB table columns have the appropriate names.

And you shouldn't use all capital letters in DB queries, or when naming your DB tables or columns. All capital words are usually for reserved query words. Example:

Code:
SELECT * FROM my_table WHERE my_column = 'something'

#3
[eluser]MightyBishop[/eluser]
Thanks for your reply, Aken. Your help is much appreciated. I will change my table fields when I can. Brilliant tip about not capitalising field names - never thought of that...

I didn't post the rest of the code, because in one case it does seem to be working and the other it doesn't - and I did not want to clutter things up.

There's not actually much more to post - the DB calls appear to work fine inside the Model code - the function is:

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

The Controller section is shown complete below - I've used the tutorial in the User Guide as a starting point as you can perhaps tell...

Code:
public function create()
{
  $this->load->helper('form');
  $this->load->library('form_validation');

  $data['title'] = 'Create a new Client';

  $data['schools'] = $this->aspirations_model->get_all_schools();
  $data['destinations'] = $this->aspirations_model->get_all_destinations();

  $this->form_validation->set_rules('FirstName', 'First Name', 'required');
  $this->form_validation->set_rules('LastName', 'Last Name', 'required');

  if ($this->form_validation->run() === FALSE)
  {

   $this->load->view('aspirations/create',$data);
  }
  else
  {
   echo("About to call Set Client");
   $this->aspirations_model->set_client();
   $this->load->view('aspirations/success');
  }
}


And the set_client() function in the View is:


Code:
<h2>&lt;?php echo $title ?&gt;</h2>

&lt;?php echo validation_errors(); ?&gt;

&lt;?php echo form_open('aspirations/create'); ?&gt;

<label for="FirstName">First Name</label>
&lt;input class="mystyles" type="input" name="FirstName" /&gt;&lt;br><br>
    
    <label for="LastName">Last Name</label>
&lt;input class="mystyles" type="input" name="LastName" /&gt;&lt;br><br>
    
<div align="left">
    
    &lt;?php //TEST CODE
     foreach ($schools as $school_item):
   echo($school_item['ID_SCHOOL']) . " ";
   echo($school_item['NAME_OF_SCHOOL']);
  endforeach;
?&gt;
     <label for="myschool">Select School</label>
  <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>
</div>
    
  <div align="left">
     <label for="mydestination">Select Destination</label>
  <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>
</div>

&lt;input class="mysubmit" type="submit" name="submit" value="Create Client" /&gt;

&lt;/form&gt;


It's not overly complicated - two selectors built up from the data sent by the two calls from the Controller to the Model, with the data sent from Controller to the View using the variable $data.

But I just get jibber from the second selector.

I am very new to PHP and MySQL, so forgive me if I am talking rubbish at any point of this. I am VERY excited by the CodeIgniter and keen to progress!

Thanks for your help

JB

#4
[eluser]TWP Marketing[/eluser]
I think I see the problem:
Code:
public function get_all_destinations()
{
  $query = $this->db->query('SELECT * FROM DESTINATIONS WHERE DELETED = 0');
  return $query->row_array();
}

Should use result_array() as:

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

#5
[eluser]MightyBishop[/eluser]
Of course! How stupid am I! Code changed, problem gone!

And the only line that uses result_array() is the one that's working!!!

How bizarre - I have been looking at that code to the micoscopic level for about TWO DAYS and I never spotted that!


Thanks so much - really appreciate you taking the time to fix that for me!

I owe you several beers!

Best regards


JB

#6
[eluser]TWP Marketing[/eluser]
Your welcome. I've made the exact same mistake, several times... I learn but slowly.

#7
[eluser]Aken[/eluser]
I missed that, too. Little things are hard to find sometimes. In the future, always check your errors carefully. If you can't see the problem on the line from the error, back track through your code to see what's going on. Commonly a good thing to do is to var_dump() the result of your model's method to see if it is returning what it is supposed to. That would've given you your answer right away. Learning to debug will save you a lot of time and hassle in the future.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.