• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loading a multidimensional array into a view

#1
[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; ?>

&lt;?php for($x = 0; $x<=count($visits); $x++): ?&gt;
<center>
<table cellspacing="0" cellpadding="5" width="50%" bgcolor="#B9AA81">
<tr><td><b>Patient number:</td>
<td>&lt;?php echo $visits[0]['patient_number']; ?&gt;</td>
<tr>
<td ><b>First name:</td>
<td>&lt;?php echo $visits[0]['first_name']; ?&gt;</td>
<tr>
<td> <b> Last name:</td>
<td>&lt;?php echo $visits[0]['last_name']; ?&gt;</td>
<tr>
<td> <b> Visit code:</td>
<td>&lt;?php echo $visits[0]['visit_code']; ?&gt;</td>
<tr>
<td> <b> Month:</td>
<td>&lt;?php echo $visits['month'];?&gt;</td>

<tr>
<td> <b> Visit date:</td>
<td>&lt;?php echo $visits['visit_date']; ?&gt;</td>
</tr>
</table>
&lt;?php $x++; ?&gt;

&lt;?php endfor; ?&gt;

I would be happy to receive any help.

#2
[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
$this->load->view(‘patient_visit’, array('visits'=>$visits));

Now, the array is extracted into the code and becomes $visits because the name of the key in the array is 'visits'.

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

#4
[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
$this->load->view(‘patient_visit’, array('visits'=>$visits));

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.

#5
[eluser]bsteve[/eluser]
Thank you it worked out so well.

#6
[eluser]bsteve[/eluser]
Thank you it worked out well


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.