CodeIgniter Forums
Pass multiple arrays/datasets into view... - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Pass multiple arrays/datasets into view... (/showthread.php?tid=49219)

Pages: 1 2


Pass multiple arrays/datasets into view... - El Forum - 02-11-2012

[eluser]dharmy[/eluser]
Hi there

I'v been working on this all afternoon but am not having much luck. Simply put, I have a view that is accepting data from 2 queries (into two different select fields).

The code for the controller - can anyone tell me where I am going wrong?

Code:
public function ShowForm()
{
  
  $this->load->model('htc_model');
  $this->load->library('parser');
  $listmobiles['country'] = $this->htc_model->GetCountryList();
  $listmobiles['mobile'] = $this->htc_model->GetMobiles();

  $home_top = $this->load->view('showform', $listmobiles, true);
  
  $data = array(
              'pagetitle'   => 'Page title goes here',
       'sitename'   => 'sitename goes here',
              'metakeywords' => 'metakeywords go here',
     'metadesc' => 'metadescription go here',
     'home_top' => $home_top,
     'top_left' => '$top_left',
     'home_middle' => '$home_middle',
     'home_bottom' => '$home_bottom',
     'footer' => '$footer'
            );

  $this->parser->parse('home_template', $data);  
}

I don't know how the view should be fetching the data. At the moment I have a country list working OK, but not the 2nd array / dataset:

Code:
<form>
<table width="60%">
<tr>
  <td><label for="country">Select Your Country</label></td>
  <td>
   <select name="country" class="country">
   <option>-- Select --</option>
   &lt;?php    
      foreach($data['country'] as $Result) {
       echo "<option value=" . $Result-&gt;country_id . ">" . $Result->country_name . "</option>\n";
      }
      //var_dump($data);
     ?&gt;  
   </select><br>  
  </td>
</tr>

<tr>
  <td>HTC Model</td>
  <td>
   <select name="model" class="model">
   &lt;?php
    
      foreach($data['mobile'] as $Result) {
       echo "<option value=" . $Result-&gt;model_ub_id . ">" . $Result->model_name . "</option>\n";
      }
      //var_dump($data);
     ?&gt;  
   </select><br>  
  </td>
</tr>



Pass multiple arrays/datasets into view... - El Forum - 02-11-2012

[eluser]CroNiX[/eluser]
I think it was recently discovered that the parser won't parse views that are returned (using the 3rd parameter, TRUE, for loading a view) because the parsing actually takes place in the output class of CI, which is bypassed when returning a view.


Pass multiple arrays/datasets into view... - El Forum - 02-11-2012

[eluser]dharmy[/eluser]
Am a bit of a newbie... so you're saying that I should set it to false? I'm still now sure how the view code would look?

How is this kind of situation normally resolved i.e. a view accepts more than one dataset from the controller/model




Pass multiple arrays/datasets into view... - El Forum - 02-11-2012

[eluser]CroNiX[/eluser]
Ohhh I see the problem.
In your controller, you are:
Code:
$listmobiles['country'] = $this->htc_model->GetCountryList();
$listmobiles['mobile'] = $this->htc_model->GetMobiles();
But, in the view, you are trying to access those variables like:
Code:
foreach($data['country'] as $Result) {

1) there is no "data" variable.
2) you access the "country" data by using just $country, not $array_variable_that_was_sent_to_the_view['country'].

When you send data to a view in an array, it runs extract() on it so you access the variables by the array key that they were stored under when sending the array to the view.

So it should be:
Code:
foreach($country as $Result) {
and the same for your other loop


Pass multiple arrays/datasets into view... - El Forum - 02-11-2012

[eluser]dharmy[/eluser]
Thanks - I gave that a go, but see the following error in Fiddler

Code:
<h4>A PHP Error was encountered</h4>

<p>Severity: Notice</p>
<p>Message:  Undefined variable: country</p>
<p>Filename: views/showform.php</p>
<p>Line Number: 10</p>

</div><div>

<h4>A PHP Error was encountered</h4>

<p>Severity: Warning</p>
<p>Message:  Invalid argument supplied for foreach()</p>
<p>Filename: views/showform.php</p>
<p>Line Number: 10</p>

</div>

The view code that causes that error is :

Code:
<td>
   <select name="country" class="country">
   <option>-- Select --</option>
   &lt;?php    
      foreach($country as $Result) {
       echo "<option value=" . $Result-&gt;country_id . ">" . $Result->country_name . "</option>\n";
      }
      //var_dump($data);
     ?&gt;  
   </select><br>  
  </td>



Pass multiple arrays/datasets into view... - El Forum - 02-11-2012

[eluser]CroNiX[/eluser]
I'm not seeing an immediate problem. In your controller, after you put the county and mobile results into the $listmobiles array, dump that array and see if the data is in the correct format. I don't understand why your loop was initially working correctly when you were using $data['country'] to begin with because the way you have it now should work and the way you had it originally shouldn't!


Pass multiple arrays/datasets into view... - El Forum - 02-11-2012

[eluser]dharmy[/eluser]
Yes it is odd... I did a var_dump() but only got the country's array not the 2nd one... Well either that or its not dumping everything to screen... Any other dump functions I can try?


Pass multiple arrays/datasets into view... - El Forum - 02-11-2012

[eluser]CroNiX[/eluser]
If you aren't getting the data back for that one, check the model. Maybe the query is bad.


Pass multiple arrays/datasets into view... - El Forum - 02-11-2012

[eluser]InsiteFX[/eluser]
He maybe returning an array when he should be returning an object.

What are you returning for these two methods in your model?
Code:
$this->htc_model->GetCountryList();
$this->htc_model->GetMobiles();



Pass multiple arrays/datasets into view... - El Forum - 02-12-2012

[eluser]dharmy[/eluser]
Hi there

This is the function returning the data from the model:

Code:
function GetMobiles()
  {
   //Query the data table for every record and row
   $this->load->database();  
  
   $query = $this->db->query('select model_ub_id, model_name from model where brand_idfk = 106 order by model_name');
  
   $this->db->close();
   return $query->result();
  
  
  }