【问题标题】:handling multiple types of arrays in php在php中处理多种类型的数组
【发布时间】:2016-04-11 07:39:42
【问题描述】:

我有多个动态文本框。

<input type="text" name="Paid_Amount[]">
<input type="text" name="Charges_Type[]">

每个文本框值在数据库中都有多行(分期付款)。 已付金额应按分期数分摊。 例如我有开发费用 = 50,000 如果开发收费,然后分期付款

分期付款表 -------------------------------------------------- ------------- ID费用_类型分期_支付金额_金额 -------------------------------------------------- ------------- 1 土地成本 10000 0 2 土地成本 10000 0 3 土地成本 10000 0 4 土地成本 10000 0 5 土地成本 10000 0 6 开发费用 5000 0 7 开发费用 5000 0

我需要将支付金额(从&lt;form&gt;获取)设置为分期付款表的支付金额

要求是:如果我在土地成本中设置12,000,那么10000应该在第一期设置,剩余的2000将在下一期设置

我尝试过使用 foreachdo while 循环,但对多个数组感到困惑:

$Charges_Type = $this->input->post('Charges_Type'); // array
$Paid_Amount = $this->input->post('Paid_Amount'); // array
$result = array of rows from installment table

如何处理这些3 arrays:

$merge = array_merge($Paid_Amount, $result);
    foreach ($merge as $key => $value)
                    {
                        if(!empty($merge[$key]))
                        {
                            foreach($result as $in)
                            {
                                if($in['Charges_Type'] == $key)
                                {
                                    $i = 0;
                                    do 
                                    {
                                        if($in['Installment_Amount'] >= $value && $in['Paid_Amount'] == 0 && $in['Charges_Type'] == $key)
                                        {
                                            echo "UPDATE `tbl_installment` SET `Paid_Amount` = '".$value."', `Paid_Date` = '".$Paid_Date."', `Is_Paid` = '1' WHERE `ID` = '".$in['ID']." AND `Charges_Type` = '".$key."''";
                                            $value = $value - $in['Installment_Amount'];
                                        }
                                        if($in['Installment_Amount'] < $value && $in['Charges_Type'] == $key)
                                        {
                                            echo "UPDATE `tbl_installment` SET `Paid_Amount` = '".$in['Installment_Amount']."', `Paid_Date` = '".$Paid_Date."', `Is_Paid` = '1' WHERE `ID` = '".$in['ID']." AND `Charges_Type` = '".$key."''";
                                            $value = $value - $in['Installment_Amount'];                        
                                        }
                                        if($i['Paid_Amount'] != 0 && $in['Charges_Type'] == $key)
                                        {
                                            $value = $value + $i['Paid_Amount'];
                                            echo "UPDATE `tbl_installment` SET `Paid_Amount` = '".$value."', `Paid_Date` = '".$Paid_Date."', `Is_Paid` = '1' WHERE `ID` = '".$in['ID']." '";
                                            $value = 0;
                                        }   
                                        $i++;
                                    } 
                                    while ($value > 0);
                                }
                            }
                        }       
                    }

【问题讨论】:

    标签: php mysql arrays codeigniter


    【解决方案1】:

    我没有为您的问题提供答案,而是另一种编写代码的方法,通过重构使其更易于阅读。这使得处理多个数组循环时更容易。

    我注意到在您的分期付款表中,您有一个拼写错误 Paid_Amout,我不确定这是否只是您发布时的拼写错误还是您的 SQL 中的错误。但这可能会导致问题。

    $merge = array_merge($Paid_Amount, $result);
    foreach ($merge as $key => $value)
    {
        if(empty($merge[$key]))
            continue;
    
        foreach($result as $in)
        {
            if($in['Charges_Type'] != $key)
                continue;
    
            $i = 0;
            do 
            {
                if($in['Installment_Amount'] >= $value && $in['Paid_Amount'] == 0 && $in['Charges_Type'] == $key)
                {
                    echo "UPDATE `tbl_installment` SET `Paid_Amount` = '".$value."', `Paid_Date` = '".$Paid_Date."', `Is_Paid` = '1' WHERE `ID` = '".$in['ID']." AND `Charges_Type` = '".$key."''";
                    $value = $value - $in['Installment_Amount'];
                }
                if($in['Installment_Amount'] < $value && $in['Charges_Type'] == $key)
                {
                    echo "UPDATE `tbl_installment` SET `Paid_Amount` = '".$in['Installment_Amount']."', `Paid_Date` = '".$Paid_Date."', `Is_Paid` = '1' WHERE `ID` = '".$in['ID']." AND `Charges_Type` = '".$key."''";
                    $value = $value - $in['Installment_Amount'];                        
                }
                if($i['Paid_Amount'] != 0 && $in['Charges_Type'] == $key)
                {
                    $value = $value + $i['Paid_Amount'];
                    echo "UPDATE `tbl_installment` SET `Paid_Amount` = '".$value."', `Paid_Date` = '".$Paid_Date."', `Is_Paid` = '1' WHERE `ID` = '".$in['ID']." '";
                    $value = 0;
                }   
                $i++;
            } while ($value > 0);
        }
    }
    

    【讨论】:

    • 没有拼写错误..你错过了第三个数组..我必须更新土地成本=土地成本开发费用=开发费用的记录
    • 我没有添加或删除任何数组。我编辑了代码以使其更易于阅读。 $result 来自哪里?
    • 是的,我知道,但我告诉你只是为了提供信息。我有 3 个数组 1. 金额 2. 费用类型(土地成本、开发费用等) 3. 查询数组(表中的记录) $query = $this->db->query("SELECT * FROM tbl_installment WHERE @987654323 @ = '".$plot_id."' AND Paid_Amount Installment_Amount AND Is_Active = '0'"); $result = $query->result_array();
    猜你喜欢
    • 2019-07-01
    • 1970-01-01
    • 2018-09-07
    • 2014-12-11
    • 1970-01-01
    • 2015-05-29
    • 1970-01-01
    • 2023-02-09
    • 1970-01-01
    相关资源
    最近更新 更多