Day 12 : PHP - 你484少给钱?如何查询货运订单是否存在

这里想和大家示范一下如何查询货运订单是否存在
还有算出帐款是否刚好,没有的话是收多还收少

首先,我们先做一个查询订单的画面,并查询订单1001是否存在

HTML:

<form action="PHP的档案位置 " method="POST">
    <table class="table_container">
        <tr>
            <td>请输入要查询的订单</td>
            <td><input type="text" name="order_search"></td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit" value="确认"></td>
        </tr>
    </table>
</form>

CSS:

.table_container {
    background-color:rgb(251, 192, 81);
    border:solid 1px #fff;
    width:330px;
    height:70px;
}
.table_container td{
    text-align:center;
    font-weight:bolder;
}

把订单资料印出来,方便我们查看结果是否正确
可以注意看我在程序码内下的注解
PHP:

//宣告客户订单资料
$order_list = array('1001' => 900, 
                    '1002' => 500, 
                    '1003' => 650, 
                    '1004' => 310, 
                    '1005' => 460, 
                    '1006' => 800, 
                    '1007' => 120, 
                    '1008' => 200,
                    '1009' => 770);
//宣告货运订单资料
$order_receive = array('1001' => 900, 
                        '1002' => 300, 
                        '1003' => 0, 
                        '1004' => 310, 
                        '1005' => 460,  
                        '1007' => 120, 
                        '1008' => 500,
                        '1009' => 770,
                        '1110' => 380);

//接收HTML传来的订单号码
$order_search = @$_POST['order_search'];

//判断$order_search是否为空值
//若是,则提醒输入订单号码
if (!empty($order_search)) {
    //印出客户订单资料
    echo "客户订单资料:<br>";
    foreach($order_list as $list_key => $list_value) {
        echo $list_key."的金额为".$list_value.'<br>';
    }
    
    echo '<br>';

    //印出货运订单资料
    echo "货运订单资料:<br>";
    foreach($order_receive as $receive_key => $receive_value) {
        echo $receive_key."的金额为".$receive_value.'<br>';
    }

    echo '<br>';

    echo "货运订单查询结果如下:<br><br>";

    //查看此订单是否在「货运」订单内
    if (array_key_exists($order_search, $order_receive)) {
        echo "货运订单".$order_search."存在<br>";
        echo "订单金额为".$order_receive[$order_search]."元<br>";
        //查看此订单是否存在於「客户」订单内
        //若在,则计算误差金额为何,否则提醒此订单为多收的订单
        if (array_key_exists($order_search, $order_list)) {
            $order_diff_money = $order_list[$order_search] - $order_receive[$order_search];
            echo "误差金额为".$order_diff_money."元<br>";
        } else {
            echo "注意:此订单为多收的订单!!";
        }
    } else {
        echo "查无".$order_search."这个订单<br>";
    }
} else {
    echo "请输入订单号码!!";
}

结果如下图所示:
https://ithelp.ithome.com.tw/upload/images/20210926/20141088dXphBYJiOK.png

可以看到订单1001在货运订单内是存在的,且金额正确
https://ithelp.ithome.com.tw/upload/images/20210926/201410883VoiAfpT8D.png

查询订单1002则显示:
https://ithelp.ithome.com.tw/upload/images/20210926/20141088vOopQ6mCIq.png

查询订单1003则显示:
https://ithelp.ithome.com.tw/upload/images/20210926/20141088LSuIpvbJ1K.png

查询订单1006则显示:
https://ithelp.ithome.com.tw/upload/images/20210926/20141088jBuqH8Lhnw.png

查询订单1008则显示:
https://ithelp.ithome.com.tw/upload/images/20210926/20141088i9G1JLXDGN.png

查询订单1110则显示:
https://ithelp.ithome.com.tw/upload/images/20210926/201410882liSMGIuXV.png


也可以找出货运没收到的「订单」,和货运没收到「货款」的订单

PHP:

//用array_diff_key取key的差集
//找出在「客户」订单内,但「货运」没收到的订单
$not_receive = array_diff_key($order_list, $order_receive);

echo "货运没有收到的订单如下:<br>";
foreach ($not_receive as $not_key => $not_value) {
    echo "订单号码".$not_key.'<br>';
}

echo '<br>';

echo "货运没有收到货款的订单如下:<br>";
foreach ($order_receive as $notMoney_key => $notMoney_value) {
    //找出货运没收到货款的订单为何
    if (empty($notMoney_value)) {
    echo "订单号码".$notMoney_key.'<br>';
    }
}

结果如下图所示:
https://ithelp.ithome.com.tw/upload/images/20210926/201410880SNZPbVCOd.png


以上就是今天的介绍

如果不太了解foreacharray_key_existsarray_diff_key……等用法
可以去Day 10看一下唷!


<<:  [Day26]Jolly Jumpers

>>:  D-19 网页站台 ? webapp ? mvc ? webapi

数据中台:实施阶段

当有新的业务需求且与既有系统的资料关联较大时,应该优先安排在中台上实现,这可以让数据中台尽快地产生业...

Day 22 - 实战演练 — Portal 系列

实作第二篇要来介绍的是 Portal 系列的实作,会一路从 Portal 实作到 Popconfi...

用React刻自己的投资Dashboard Day24 - styled components

tags: 2021铁人赛 React 因为下一篇要让导览列在手机版网页呈现汉堡选单的样式,在参考其...

Day 27 - Click and Drag to Scroll

前言 JS 30 是由加拿大的全端工程师 Wes Bos 免费提供的 JavaScript 简单应用...

Mac机必备--超实用软件推荐

当你第一次购买 Mac 时,除了学习和适应使用之外,你还探索了击键技巧。 这时候,你需要用好软件才有...