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

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?

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

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.

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 ?

if you need to use the number, you can do:

$num =  count($books_array);

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

[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