$query = "SELECT encounter,amount1,amount2,posted1,posted2 FROM payments WHERE pid='$patient_id'";
$res = mysql_query($query);
$max =25;
$num_rows=mysql_num_rows($res);
$x_count=0;
while($row=sqlFetchArray($res)){
$encounter_id=$row['encounter'];
//$method=$row['method'];
$amount1=floatval($row['amount1']);
$amount2=floatval($row['amount2']);
$posted1=floatval($row['posted1']);
$posted2=floatval($row['posted2']);
$total_amount_paid=floatval($amount1+$amount2+$posted1+$posted2);
for($x_count=0;$x_count<$num_rows;$x_count++){
//$encounter_id[$x_count]=$encounter_id;
$query = "SELECT date,SUM(fee) AS totalcharges FROM billing WHERE pid='$patient_id' AND encounter=$encounter_id";
$res = mysql_query($query);
while($row=sqlFetchArray($res)){
$date_fee=$row['date'];
$total_charges=floatval($row['totalcharges']);
$balance=floatval($total_charges-$total_amount_paid);
$pdf->SetFillColor(254,254,254);
$pdf->Ln();
$pdf->SetX(10);
$pdf->Cell(50,4,$date_fee,1,0,'L',1);
$pdf->Cell(40,4,$encounter_id,1,0,'L',1);
$pdf->Cell(40,4,$total_charges,1,0,'L',1);
$pdf->Cell(40,4,$total_amount_paid,1,0,'L',1);
$pdf->Cell(40,4,$balance,1,0,'L',1);}
}
}
The above code shows my attempt to fetch key called encounter from table called payments. for each encounter, i want to obtain records(many) from another table called billing. i want to sum some values based on an encounter.
the above code returns only one row and i have over 6 rows that i expect to see. where is the problem ? can somebody help me ?