CodeIgniter Forums
Forwarding DB array 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: Forwarding DB array to view (/thread-20932.html)



Forwarding DB array to view - El Forum - 07-26-2009

[eluser]HooJee[/eluser]
Hi Guys

I am trying to execute a DB query and pass an array onto the view for processing. How exactly would I pass the DB array from the controller to the view?


Forwarding DB array to view - El Forum - 07-26-2009

[eluser]Derek Allard[/eluser]
Something like this

Code:
function pass_array()
{
    $book_query = $this->db->get('books');

    $data['books_array'] = $book_query->result_array();

    $this->load->view('view_file', $data);
}
Don't forget to count your results (num_rows()) in case the query doesn't bring anything back.


Forwarding DB array to view - El Forum - 07-26-2009

[eluser]HooJee[/eluser]
Thanks.
I know I can use the num_rows() method in the controller but how would I count the size of the array in the view ?


Forwarding DB array to view - El Forum - 07-26-2009

[eluser]wowdezign[/eluser]
if you need to use the number, you can do:

Code:
$num =  count($books_array);

but if you just want to iterate:
Code:
foreach($books_array as $item){
  // each time through do something;
}



Forwarding DB array to view - El Forum - 07-26-2009

[eluser]Derek Allard[/eluser]
Thanks wowdezign. Yep, at that point its just a regular PHP array so count is the way to go.

I will mention though that typically you'd want to do that type of logic in the controller and not in the view, but then again I don't have a good understanding of your design goals, so that's just a general statement.