CodeIgniter Forums
Passing a data array from model to controller and then finally to view - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Passing a data array from model to controller and then finally to view (/thread-53620.html)



Passing a data array from model to controller and then finally to view - El Forum - 07-31-2012

[eluser]Unknown[/eluser]
Pretty basic stuff and probably have been asked a thousand times, but can't seem to find a simple answer about this.

This is an example of what I'm trying to do :

controller
Code:
$this->load->model('model_name', '', TRUE);
$data['something'] = $this->model_name->getAllSomethings();
$this->load->view('Index_view', $data);

model
Code:
function getAllSomethings()
{
  $query = $this->db->query("SELECT something FROM tblTable");

  foreach ($query->result_array() as $row)
  {
   return $row['something'];
  }
}
view
Code:
foreach ($something as $row)
{
  echo '$row['something'].'<br/>';
}

Obviously does not work.
How exactly do you send a data array and then echo everything out.


Passing a data array from model to controller and then finally to view - El Forum - 07-31-2012

[eluser]mrh[/eluser]
Ok I'm new too so this may have errors. In my model:

Code:
// Define values to send back
$data = array();
$data['appCount'] = 0;
$data['appData'] = array();

// Run query
  // Run the query
  $query = $this->db->query( $theQuery );
  $count = $query->num_rows();
  if( $count>0 )
  {
   $data['appCount'] = $count;
  
   foreach( $query->result() as $row )
   {
                               // Note stuffing each row as an array in my $data['appData'] return
    $data['appData'][$row->id] = $row->projectName;
   }    
  }

  return $data;

So that is in the model.

In the controller:

Code:
// Get admin portal data
   $data = $this->data_model->getAdminPortalData();
      
   // Load the view
   $this->load->view( 'header', $menuData );
   $this->load->view( 'admin_view', $data );
   $this->load->view( 'footer', $menuData );

Finally in the view. In this case I'm specifically populating the array in the model return so that I can use it in a drop down list on a form:

Code:
<div id="portalblockinfo">
    &lt;?php echo form_open( 'admin_portal/editappl' ); ?&gt;
     Select application to edit: &lt;?php echo form_dropdown('activeapps', $appData, '' ); ?&gt;
     <br>  
     &lt;input id="submit" type="submit" value="Go" /&gt;
    &lt;?php echo form_close(); ?&gt;
   </div>

But I could have easily done a php for to access the data in the array

Code:
&lt;?php for($i=0; $i<count($appData); $i++) { ?&gt;
&lt;?php echo $appData[$i][1] ?&gt;
&lt;?php } ?&gt;

I did $appData[$i][1] to get the projectName from the array. See the query above. Obviously just echoing the array contents will result in everything being on the same line. So you could add a <br> after each echo.

The basic difference between my code and yours is that in my model I create the array as a separate element of the $data passed to the view.


Passing a data array from model to controller and then finally to view - El Forum - 07-31-2012

[eluser]Aken[/eluser]
You need to return the result array in your model. Your code now is only returning the value of the "something" column in the first row of the result set.