在while循环中查询获取陈旧数据

时间:2016-01-15 22:34:19

标签: php mysql loops while-loop

我正在尝试(而不是成功)运行带有查询的while循环,该查询获取表中的最后一行,使用该数据然后创建一个新行,然后再次选择表中的最后一行(应该是刚创建的行然后使用该数据并创建一个新行。这应该重复,直到while循环不再为真。

我遇到的问题是while循环运行但始终使用它第一次选择的行。因此,同一行会一遍又一遍地插入,直到while循环为假。

我的问题是,当while循环开始一个新循环时,如何让查询刷新?

我已经尝试了unset(),但这不起作用,而且没有想法。这里是我的代码的while循环(它仍然在进行中 - 仍然需要添加): -

while ($total_debt > 0) {
    $get_dp_accounts_list_query = "SELECT account_id
        FROM money_debt_planner
        WHERE customer_id = '".$_SESSION['customer_id']."'
        GROUP BY account_id";
    $get_dp_accounts_list = $db->Execute($get_dp_accounts_list_query);
    while (!$get_dp_accounts_list->EOF) {
        $get_dp_accounts_listArray[] = array('account_id'=>$get_dp_accounts_list->fields['account_id']);
        $get_dp_accounts_list->MoveNext();
    }
    foreach($get_dp_accounts_listArray as $acc_list) {
        $get_last_dp_entry_query = "SELECT *
            FROM money_debt_planner
            WHERE customer_id = '".$_SESSION['customer_id']."'
            AND account_id = '".$acc_list['account_id']."'
            ORDER BY line_id DESC";
        $get_last_dp_entry = $db->Execute($get_last_dp_entry_query);

        if($get_last_dp_entry->fields['end_debt_balance'] <> 0) {
            // calculate the interest this period
            $accounts_balance = $get_last_dp_entry->fields['end_debt_balance'] + $get_last_dp_entry->fields['estimated_spending'];

            $min_pay = $get_last_dp_entry->fields['min_pay_amount'];
            $min_pay_rate = $get_last_dp_entry->fields['min_pay_rate'];
            $interest_rate = $get_last_dp_entry->fields['interest_rate'];

            $int_rate = $interest_rate /100;
            $int_rate_a = $int_rate + 1;
            $int_value_b = $accounts_balance ;
            $int_value_c = ($int_rate_a * $int_value_b) - $int_value_b . '       ';
            $int_value_d = $int_value_c / 12;

            $statement_balance = $accounts_balance + $int_value_d;

            $min_pay_rate = ($statement_balance) * $min_pay_rate / 100;

            if($min_pay_rate < $min_pay) {
                $new_bill_amount = $min_pay;
            } else { 
                $new_bill_amount = $min_pay_rate;
            }
            if(($statement_balance) <= ($min_pay_rate) && ($statement_balance) <= $min_pay) { 
                $new_bill_amount = $statement_balance;
            }

            // next pay date 
            $next_due_day1 = date('d', strtotime ( $get_last_dp_entry->fields['date'] ));
            $next_due_year_month1 = date('Y-m', strtotime ( $get_last_dp_entry->fields['date'] ));

            $next_due_month_a_1 = strtotime ( "+ 1 month" , strtotime ( $next_due_year_month1 ) ) ;
            $next_due_month_b_1 = date ( 'Y-m' , $next_due_month_a_1);
            $total_days_in_this_month = date('t', strtotime($next_due_month_b_1) );

            if($next_due_day1 >= $total_days_in_this_month) {
                $next_due_date_a_1 = $total_days_in_this_month;
            } else { 
                $next_due_date_a_1 = $next_due_day1;
            }

            $payment_date_from_account1 = $next_due_month_b_1 .'-' . $next_due_date_a_1;
            $next_due1a = strtotime ( $payment_date_from_account1 ) ;
            $next_due1 = date ( 'Y-m-d' , $next_due1a );

            $dp_pay_date = $next_due1; // this will be 1 month from last entry
            $interest_this_period = $int_value_d; // this will be interest on last end balance + est spending
            $payment_this_period = $new_bill_amount; // this will be the min payment allowed including any over credit amount
            $end_balance = ''; // this will be current open balance + spending + interest - payment

            $sql = "INSERT INTO `money_debt_planner` (`customer_id`, `account_id`, `date`, `open_debt_balance`, `interest_rate`, `min_pay_rate`, `min_pay_amount`, `credit_limit`, `estimated_spending`, `interest_this_period`, `payment_this_period`, `end_debt_balance`)
            VALUES ('".$_SESSION['customer_id']."', '".$acc_list['account_id']."', '".$dp_pay_date."', '".$get_last_dp_entry->fields['end_debt_balance']."', '".$get_last_dp_entry->fields['interest_rate']."', '".$get_last_dp_entry->fields['min_pay_rate']."', '".$get_last_dp_entry->fields['min_pay_amount']."', '".$get_last_dp_entry->fields['credit_limit']."', '".$get_last_dp_entry->fields['estimated_spending']."', '".$interest_this_period."', '".$payment_this_period."', '".$end_balance."')";
            $db->Execute($sql);
        } // end if balance zero
    } // end account list

    // to do this will be total debt balance utstanding
    $total_debt = $total_debt - 1000;
}

您可以提供的任何帮助都很棒:o)

0 个答案:

没有答案