Hello, would someone look at my code and advise how to change. The amt payable minus the amt paid drops the cents above ".00". Below is example of the problem and below that the pertinent code. $totdue = $totdue + $row['amtdue']; $totpaid = $totpaid + $row['paidamt']; $totowed = $totdue - $totpaid; $amtdue = str_replace(',', '.', $row['amtdue']); $paidamt = str_replace(',', '.', $row['paidamt']); $due = $row['amtdue'] - $row['paidamt']; $due = str_replace('.', ',', $due); if ($late > 120) { $row['dayslate'] = "pastdue"; } // $row['duedate'] = preg_replace('/^[0:-]*$/', '', $row['duedate']); // $row['datepaid'] = preg_replace('/^[0:-]*$/', '', $row['datepaid']); // if(str_replace(array('0','-',':'),' ',$row['dayslate'])=>'120');{$row['dayslate']='past due';} echo "<tr>"; echo "<td>" . $row['status'] . "</td>"; echo "<td>" . $row['acctno'] . "</td>"; echo "<td>" . $row['bname'] . "</td>"; echo "<td>" . $row['purpose'] . "</td>"; echo "<td>" . $row['duedate'] . "</td>"; echo "<td>" . $row['datepaid'] . "</td>"; echo "<td align=right>" . $row['dayslate'] . "</td>"; echo "<td align=right>" . $row['amtdue'] . "</td>"; echo "<td align=right>" . $row['paidamt'] . "</td>"; echo '<td>', number_format($due, 2, '.', ''), '</td>'; } echo "<tr>"; echo "<td>Gtotals</td>"; echo "<td colspan=6></td>"; echo "<td align=right>" . sprintf("%.2f",$totdue) . "</td>"; echo "<td align=right>" . sprintf("%.2f",$totpaid) . "</td>"; echo "<td align=right>" . sprintf("%.2f",$totowed) . "</td>"; echo "</tr>"; echo "</table>"; mysql_close(); ?> </body></html> PHP: </body></html>
1) might help if you had FORMATTING to make your code legible. Some indentation might help us recognize this starts out in the middle of a while.. 2) would REALLY help if you stopped using multiple echo to do one echo's job. 3) you seem to be making variables you aren't even going to use. 4) since your totals probably belongs in a TFOOT, and TFOOT should be before TBODY, I'd be tempted to iterate the row list twice; either that or I'd run two queries, one to do the totals and one for the rows. 5) I'd also suggest you STOP using double quotes and string additions to do single quotes and comma delimit's job. 6) didn't we just discuss this? Use number_format, not sprintf. What you probably meant to do was something like this: $totdue += $row['amtdue']; $totpaid += $row['paidamt']; echo ' <tr> <td>', $row['status'], '</td> <td>', $row['acctno'], '</td> <td>', $row['bname'], '</td> <td>', $row['purpose'], '</td> <td>', $row['duedate'], '</td> <td>', $row['datepaid'], '</td> <td class="currency">', ($late > 120 ? 'pastdue' : $row['dayslate']), '</td> <td class="currency">', number_format($row['amtdue'], 2, '.', ''), '</td> <td class="currency">', number_format($row['paidamt'], 2, '.', ''), '</td> <td>', number_format($row['amtdue'] - $row['paidamt'], 2, '.', ''), '</td> </tr>'; } echo ' <tr> <th scope="row" colspan="7">Grand Total:</th> <td class="currency">', number_format($totdue, 2, '.', ''), '</td> <td class="currency">', number_format($totpaid, 2, '.', ''), '</td> <td class="currency">', number_format($totdue-$totpaid, 2, '.', ''), '</td> </tr> </table>'; Code (markup):
Happy Birthday. <?php /* missing code above this */ $totdue = $totdue + $row['amtdue']; $totpaid = $totpaid + $row['paidamt']; $totowed = $totdue - $totpaid; $amtdue = str_replace(',', '.', $row['amtdue']); $paidamt = str_replace(',', '.', $row['paidamt']); $due = $row['amtdue'] - $row['paidamt']; $due = str_replace('.', ',', $due); if ($late > 120) { $row['dayslate'] = 'pastdue'; } echo ' <tr> <td>' . stripslashes($row['status']) . '</td> <td>' . $row['acctno'] . '</td> <td>' . stripslashes($row['bname']) . '</td> <td>' . stripslashes($row['purpose']) . '</td> <td>' . $row['duedate'] . '</td> <td>' . $row['datepaid'] . '</td> <td align="right">' . $row['dayslate'] . '</td> <td align="right">' . $row['amtdue'] . '</td> <td align="right">' . $row['paidamt'] . '</td> <td>' . number_format($due, 2, '.', ',') . '</td> '; } echo ' <tr> <td>Gtotals</td> <td colspan="6"></td> <td align="right">' . number_format($totdue, 2, '.', ',') . '</td> <td align="right">' . number_format($totpaid, 2, '.', ',') . '</td> <td align="right">' . number_format($totowed, 2, '.', ',') . '</td> </tr> </table> </body> </html> '; /* PHP auto closes mysql connection at the end. */ ?> PHP: