简体   繁体   English

在PHP中处理多种类型的数组

[英]handling multiple types of arrays in php

I have multiple dynamic text boxes. 我有多个动态文本框。

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

Each text box value have multiple rows (installments) in database. 每个文本框值在数据库中都有多行(分期付款)。 Paid Amount should be divided according to number of installments. 支付金额应根据分期付款次数进行划分。 for example i have development charges = 50,000 and then installments if development charges 例如我有开发费= 50,000 ,然后如果开发费分期付款

Installment table
---------------------------------------------------------------
ID   Charges_Type             Installment_Amount    Paid_Amout      
---------------------------------------------------------------
1    Land Cost                10000                   0                
2    Land Cost                10000                   0            
3    Land Cost                10000                   0             
4    Land Cost                10000                   0          
5    Land Cost                10000                   0
6    Development Charges      5000                    0
7    Development Charges      5000                    0

I need to set Paid amount (getting from <form> ) to installment table's paid amount 我需要将支付金额(从<form>获取)设置为分期付款表的支付金额

the requirement is: if i set 12,000 in land cost then 10000 should be set in first installment and remaining 2000 will be set in next installment 要求是:如果我将土地成本设置为12,000 ,则第一期应设置为10000 ,下一部分应设置为剩余的2000

i have tried with foreach and do while loop but getting confused with multiple arrays: 我已经尝试过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

how to handle these 3 arrays : 如何处理这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);
                                }
                            }
                        }       
                    }

I haven't provided an answer to your question, but an alternative way of writing your code to make it easier to read by refactoring it. 我没有为您的问题提供答案,但是提供了一种替代方法来编写代码,以使其通过重构更易于阅读。 That makes it easier when working with multiple array loops. 这使得在处理多​​个数组循环时更加容易。

I noticed in your table, Installment table, you have a misspelling, Paid_Amout, I'm not sure if that's just a misspelling when you posted it or if it's an error in your SQL. 我在您的表(安装表)中注意到,您拼写错误,Paid_Amout,我不确定发布时是否只是拼写错误,或者您的SQL中有错误。 But that could lead to issues. 但这可能会导致问题。

$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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM