![]() |
Loading a multidimensional array into a view - 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: Loading a multidimensional array into a view (/showthread.php?tid=30011) |
Loading a multidimensional array into a view - El Forum - 04-29-2010 [eluser]bsteve[/eluser] Hello to all, i am developing my end of year project but i am stack some where, I would like to generate reports from a result set created as a result of joining two tables. this is the query. $conn = Doctrine_Manager::connection(); $result= $conn->execute(' select p.*,v.* from Patient p INNER JOIN Visit v ON p.patient_number = v.patient_number'); $visits = $result->fetchAll(); $this->load->view('patient_visit', $visits); Every time i try to manipulate the array in the view i get an error message that. use of undefined variable visits. below is how i am doing it in the view. <?php $x =0; ?> <?php for($x = 0; $x<=count($visits); $x++): ?> <center> <table cellspacing="0" cellpadding="5" width="50%" bgcolor="#B9AA81"> <tr><td><b>Patient number:</td> <td><?php echo $visits[0]['patient_number']; ?></td> <tr> <td ><b>First name:</td> <td><?php echo $visits[0]['first_name']; ?></td> <tr> <td> <b> Last name:</td> <td><?php echo $visits[0]['last_name']; ?></td> <tr> <td> <b> Visit code:</td> <td><?php echo $visits[0]['visit_code']; ?></td> <tr> <td> <b> Month:</td> <td><?php echo $visits['month'];?></td> <tr> <td> <b> Visit date:</td> <td><?php echo $visits['visit_date']; ?></td> </tr> </table> <?php $x++; ?> <?php endfor; ?> I would be happy to receive any help. Loading a multidimensional array into a view - El Forum - 04-29-2010 [eluser]mddd[/eluser] The values you send to the view are 'extracted'. That means if you send an array( 'name'=>'Pete', 'age'=>42) to the view, in the view you will use $name and $age. SO, the array you send, is changed to separated variables in the view. An easy way to fix your code is: Code: // in the model Now, the array is extracted into the code and becomes $visits because the name of the key in the array is 'visits'. Loading a multidimensional array into a view - El Forum - 04-29-2010 [eluser]Jondolar[/eluser] you are incrementing the $x variable twice, once in the for loop and one at the end of the for loop. This is causing your last record to be out of range. Loading a multidimensional array into a view - El Forum - 04-29-2010 [eluser]Ivan A. Zenteno[/eluser] [quote author="mddd" date="1272567528"]The values you send to the view are 'extracted'. That means if you send an array( 'name'=>'Pete', 'age'=>42) to the view, in the view you will use $name and $age. SO, the array you send, is changed to separated variables in the view. An easy way to fix your code is: Code: // in the model Now, the array is extracted into the code and becomes $visits because the name of the key in the array is 'visits'.[/quote] I agree, remember: In the controller you have $visits, but in the view does't exists $visits var, do the example before described. Loading a multidimensional array into a view - El Forum - 04-30-2010 [eluser]bsteve[/eluser] Thank you it worked out so well. Loading a multidimensional array into a view - El Forum - 04-30-2010 [eluser]bsteve[/eluser] Thank you it worked out well |