Welcome Guest, Not a member yet? Register   Sign In
Forwarding DB array to view
#1

[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?
#2

[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.
#3

[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 ?
#4

[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;
}
#5

[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.




Theme © iAndrew 2016 - Forum software by © MyBB