我有以下php / html代码:

<div id="demo">  
    <table cellpadding="0" cellspacing="0" border="0" class="display" id="example">
        <thead>
            <tr>
                <th>Medicine Name</th>
                <th>Batch Number</th>
                <th>Total Quantity</th>
                <th>Expiry Date(s)</th>
                <th>Selling Price</th>
                <th> Total Price</th>
                <th>Issue</th>
            </tr>
        </thead>

        <tbody>
<?php foreach ($prescription as $prescribed): ?>
                <tr class="odd gradeX">
                    <td><?php echo $prescribed['commodity_name']; ?></td>
                    <td ><?php echo $prescribed['batch_no']; ?></td>
                    <td><?php echo $prescribed['total_quantity']; ?></td>
                    <td><?php echo $prescribed['expiry_date']; ?></td>
                    <td ><?php echo $prescribed['selling_price']; ?></td>
                    <td><?php  

                    $total_quantity = $prescribed['total_quantity'];
                    $selling_price = $prescribed['selling_price'];
                    $total_quantity_float = floatval($total_quantity);
                    $selling_price_float = floatval($selling_price);
                    $total_price = $total_quantity_float*$selling_price_float;
                    echo $total_price;

                    ?></td>
                    <td>  
                        <a class="issue" href="#types"  id="issue">Issue</a>
                        <input type="hidden" name="batch_no" id="batch_no" value="<?php echo $prescribed['batch_no']; ?>"/>
                    </td>
    <!--                        <td> <a id="issue1" class="issue1" href="#types">Issue</a> </td>-->
                </tr>
<?php endforeach; ?>
        </tbody>
    </table>

</div>


我想获取total_price变量的总和,该变量在表中显示为一行(total_price变量)。这应该显示所有商品的总价,我该怎么做呢?

最佳答案

尝试这个

<?php
$total_price_sum = 0; 
foreach ($prescription as $prescribed){
    ..
    ..
    $total_price_sum = $total_price_sum + $total_price;
}
echo $total_price_sum;


?>

08-04 05:13