iT邦幫忙

2021 iThome 鐵人賽

DAY 12
0
自我挑戰組

大二萌新的學習紀錄系列 第 12

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看一下唷!


上一篇
Day 11 : PHP - 如何將HTML的內容傳送到PHP?POST和GET又該如何選擇?
下一篇
Day 13 : PHP - 當陣列中有兩個重複的key值,該如何將它們的value全部印出?
系列文
大二萌新的學習紀錄30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言