减去两个总数

时间:2013-02-20 14:26:19

标签: php subtraction totals

我有一个通过mysql链接的表。我有5列显示:名称,商店,描述,欠款和成本。我写了一个脚本来计算“欠款”和“费用”的总和。我无法从总金额中扣除“总金额”。

以下是我的代码。

<div>
        <table id="datatables" class="display">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Shop</th>
                    <th>Description</th>
        <th>Amount due</th>
        <th>Cost</th>
       </tr>
            </thead>
            <tbody>
                <?php
                while ($row = mysql_fetch_array($result)) {
                    ?>
                    <tr>
                        <td><?=$row['name']?></td>
                        <td><?=$row['category']?></td>
                        <td><?=$row['subject']?></td>
                        <td><?=$row['custom1']?></td>
                        <td><?=$row['custom2']?></td>
              </tr>
                    <?php
                }
                ?>

                <?php
                    $query = "SELECT custom1, SUM(custom1) FROM hesk_tickets";

                    $result = mysql_query($query) or die(mysql_error());

                    while($row = mysql_fetch_array($result)){
                    echo "Total Owed". $row['custom1']. " = £". $row['SUM(custom1)'];
                    echo "<br />";
                }

                ?>  

                <?php
                    $query = "SELECT custom2, SUM(custom2) FROM hesk_tickets";

                    $result = mysql_query($query) or die(mysql_error());

                    while($row = mysql_fetch_array($result)){
                    echo "Cost exVAT". $row['custom2']. " = £". $row['SUM(custom2)'];
                    echo "<br />";
                    echo "Profit". $row['custom1 , custom2']. " = £". $row['SUM(custom1 - custom2)'];
                    echo "<br />";
                }

                ?>  

            </tbody>
        </table>
    </div>

非常感谢任何帮助

1 个答案:

答案 0 :(得分:0)

这是问题所在:

echo "Profit". $row['custom1 , custom2']. " = £". $row['SUM(custom1 - custom2)'];

需要:

echo "Profit". $row['custom1']. ",".$row['custom2']." = £". $row['custom1'] - $row['custom2'];

并注意来自@ h2ooooooo的建议 - 去查找使用PHP / MySQL的更好,更安全的方法。