CodeIgniter Forums
multiple query results in a view - HELP - 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: multiple query results in a view - HELP (/showthread.php?tid=1885)



multiple query results in a view - HELP - El Forum - 07-03-2007

[eluser]woracal[/eluser]
Hi all,

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

thanks!


multiple query results in a view - HELP - El Forum - 07-03-2007

[eluser]Unknown[/eluser]
in your controller :

Code:
$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 :

Code:
<?php foreach($result as $row): ?>
  <?= $row->field1 ?>
<?php endforeach; ?>
...
<?php foreach($other_result as $row): ?>
  <?= $row->field1 ?>
<?php endforeach; ?>



multiple query results in a view - HELP - El Forum - 07-03-2007

[eluser]woracal[/eluser]
thanks a ton!


multiple query results in a view - HELP - El Forum - 07-03-2007

[eluser]obiron2[/eluser]
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.


multiple query results in a view - HELP - El Forum - 07-06-2007

[eluser]Jamongkad[/eluser]
Nice how would it be possible if you want to display the query from MYSQL in reverse chronological order. Let's say you make a blogging application and you want to have the newest posts displayed at the top of the page first.


multiple query results in a view - HELP - El Forum - 07-06-2007

[eluser]obiron2[/eluser]
You have two choices.

The first would be to use ksort or usort to sort the data (look it up, there is enough on this board and the interweb to make it work, or the better way would be to sort the results as part of the SQL query.

Code:
Select * from Blogfile where blogger = 'Joe Smith' order by entrydate DESC.

The DESC bit says sort descending.

The more manipulation you can do at SQL level, the less you have to muck around with in your controller or your method properties (that should set the cat amongst the pigeons...)


multiple query results in a view - HELP - El Forum - 07-06-2007

[eluser]Jamongkad[/eluser]
[quote author="obiron2" date="1183727715"]You have two choices.

The first would be to use ksort or usort to sort the data (look it up, there is enough on this board and the interweb to make it work, or the better way would be to sort the results as part of the SQL query.

Code:
Select * from Blogfile where blogger = 'Joe Smith' order by entrydate DESC.

The DESC bit says sort descending.

The more manipulation you can do at SQL level, the less you have to muck around with in your controller or your method properties (that should set the cat amongst the pigeons...)[/quote]

I totally agree with you with the data manipulation on the SQL level.

Thanks man I tried googling the topic but couldn't find any(I guess I just keyed in the wrong search words) The code work with a little Active Record magic.


multiple query results in a view - HELP - El Forum - 07-06-2007

[eluser]obiron2[/eluser]
The problem with defining sorting and grouping criteria within the SQL is that you now need two functions (or a complicated function)if you need to sort the data in different ways.

If you group and sort raw data sets outside of the SQL then you keep the models simple (but sacrifice speed)


multiple query results in a view - HELP - El Forum - 04-21-2011

[eluser]Rufus09[/eluser]
Thanks allot for this information. I am coming back to CI after a couple years in hiatus, and have forgotten a lot of what I knew, not that I was exactly a pro to begin with. I am finding a lot of great threads here which are really helping me get back up to speed. I am very grateful for this forum.