Monday, February 14, 2011

multiple query results in a view - HELP


I need to use multiple query results in a single view. I’m quite new to CI. Appreciate some help/pointers to sample code.

in your controller :

$result $this->db->get('my_table')->result();
...
$other_result $this->db->get('my_other_table')->result();
...
$this->load->view('my_view',array('result' => $result 
'other_result' => $other_result); 
 
in your view :

<?php foreach($result as $row): ?>
  <?
$row->field1 ?>
<?php 
endforeach; ?>...<?php foreach($other_result as $row): ?>
  <?
$row->field1 ?>
<?php 
endforeach; ?> 
 
 
 thanks !
 
 
Same principle, but may make your code easier to read (personal preferance really)

$pass_to_view[‘result’]= $this->db->get(‘my_table’)->result();
...
$pass_to_view[‘other_result’] = $this->db->get(‘my_other_table’)->result();
...
$this->load->view(‘my_view’,$pass_to_view);
$pass_to_view is then a key=>value array and when it gets to the view, each key will automatically be available as its own variable / array and the view code will be exactly the same.
 

No comments:

Post a Comment