PHP笔记-订货单例子

x33g5p2x  于2021-12-30 转载在 PHP  
字(1.3k)|赞(0)|评价(0)|浏览(409)

程序运行截图如下:

源码如下:

<?php
    $goods = [
        ["name" => "主板", "price" => 979, "producing" => "广州", "num" => 8],
        ["name" => "显卡", "price" => 1599, "producing" => "上海", "num" => 19],
        ["name" => "硬盘", "price" => 579, "producing" => "广州", "num" => 21],
        ["name" => "CPU", "price" => 2999, "producing" => "北京", "num" => 31],
    ];
?>

<title>商品订货单</title>
<table border="1" cellpadding="1" cellspacing="1">
    <tr>
        <td colspan="5">商品订货单</td>
    </tr>
    <tr>
        <td>商品名称</td>
        <td>价格(元)</td>
        <td>产地</td>
        <td>数量(件)</td>
        <td>总价(元)</td>
    </tr>
    <?php
        $total = 0;
        foreach ($goods as $values){

            $str = "<tr>";
            foreach ($values as $v){

                $str .= "<td>" . $v . "</td>";
            }
            $sum = $values["price"] * $values["num"];
            $str .= "<td>" . $sum . "</td>";
            $str .= "</tr>";
            echo $str;

            $total += $sum;
        }

        $str = "<tr><td colspan='5'>小计: " . $total . "元</td> </tr>";
        echo $str;
    ?>
</table>

相关文章