need help w/code

Discussion in 'PHP' started by ataloss, Mar 16, 2014.

  1. #1
    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>
     
    ataloss, Mar 16, 2014 IP
  2. samyak

    samyak Active Member

    Messages:
    280
    Likes Received:
    7
    Best Answers:
    4
    Trophy Points:
    90
    #2
    What is this for:
    $due = str_replace('.', ',', $due);
    PHP:
    . That doesn't seem right at all.
     
    samyak, Mar 16, 2014 IP
    DomainerHelper likes this.
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #3
    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):
     
    deathshadow, Mar 17, 2014 IP
    DomainerHelper likes this.
  4. DomainerHelper

    DomainerHelper Well-Known Member

    Messages:
    445
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    100
    #4
    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:
     
    DomainerHelper, Mar 18, 2014 IP