插入月份并使用金额更新

时间:2015-09-05 01:38:55

标签: php mysql

我不知道,我是如何做到这一点的,如果有人可以提供帮助,我将不胜感激。

首先,如果客户说她将在5个月内支付一定金额,我希望能够插入每月支付金额的月份。

例如,自9月起五个月,金额为每月30个月。所以我应该能够像table1一样插入到mysql数据库中。我正在使用php

表1

<table border="1" cellspacing="0" cellpadding="0">
  <tr>
    <td width="30" valign="top"><p>#</p></td>
    <td width="138" valign="top"><p>date</p></td>
    <td width="126" valign="top"><p>Amount</p></td>
    <td width="72" valign="top"><p>Status</p></td>
  </tr>
  <tr>
    <td width="30" valign="top"><p>1</p></td>
    <td width="138" valign="top"><p>01/10/2015</p></td>
    <td width="126" valign="top"><p>30</p></td>
    <td width="72" valign="top"><p>unpaid</p></td>
  </tr>
  <tr>
    <td width="30" valign="top"><p>2</p></td>
    <td width="138" valign="top"><p>01/11/2015</p></td>
    <td width="126" valign="top"><p>30</p></td>
    <td width="72" valign="top"><p>unpaid</p></td>
  </tr>
  <tr>
    <td width="30" valign="top"><p>3</p></td>
    <td width="138" valign="top"><p>01/12/2015</p></td>
    <td width="126" valign="top"><p>30</p></td>
    <td width="72" valign="top"><p>unpaid</p></td>
  </tr>
  <tr>
    <td width="30" valign="top"><p>4</p></td>
    <td width="138" valign="top"><p>01/01/2016</p></td>
    <td width="126" valign="top"><p>30</p></td>
    <td width="72" valign="top"><p>unpaid</p></td>
  </tr>
  <tr>
    <td width="30" valign="top"><p>5</p></td>
    <td width="138" valign="top"><p>01/02/2016</p></td>
    <td width="126" valign="top"><p>30</p></td>
    <td width="72" valign="top"><p>unpaid</p></td>
  </tr>
</table>

对于此表,我不知道如何从10月开始插入接下来五个月的日期。

然后第二个挑战是,如果她进来支付说65我应该能够更新表格在三个月内传播65

<table border="1" cellspacing="0" cellpadding="0">
  <tr>
    <td width="30" valign="top"><p>#</p></td>
    <td width="138" valign="top"><p>date</p></td>
    <td width="126" valign="top"><p>Amount Due</p></td>
    <td width="72" valign="top"><p>Status</p></td>
  </tr>
  <tr>
    <td width="30" valign="top"><p>1</p></td>
    <td width="138" valign="top"><p>01/10/2015</p></td>
    <td width="126" valign="top"><p>30</p></td>
    <td width="72" valign="top"><p>Paid</p></td>
  </tr>
  <tr>
    <td width="30" valign="top"><p>2</p></td>
    <td width="138" valign="top"><p>01/11/2015</p></td>
    <td width="126" valign="top"><p>30</p></td>
    <td width="72" valign="top"><p>Paid</p></td>
  </tr>
  <tr>
    <td width="30" valign="top"><p>3</p></td>
    <td width="138" valign="top"><p>01/12/2015</p></td>
    <td width="126" valign="top"><p>25</p></td>
    <td width="72" valign="top"><p>Unpaid</p></td>
  </tr>
  <tr>
    <td width="30" valign="top"><p>4</p></td>
    <td width="138" valign="top"><p>01/01/2016</p></td>
    <td width="126" valign="top"><p>30</p></td>
    <td width="72" valign="top"><p>Unpaid</p></td>
  </tr>
  <tr>
    <td width="30" valign="top"><p>5</p></td>
    <td width="138" valign="top"><p>01/02/2016</p></td>
    <td width="126" valign="top"><p>30</p></td>
    <td width="72" valign="top"><p>Unpaid</p></td>
  </tr>
</table>

1 个答案:

答案 0 :(得分:0)

这是一个关于如何执行此操作的示例脚本,正如我在之前的评论中所解释的那样。您所要做的就是熟练掌握数据库并输出到它:

<?php

$costs = 180; //Total costs
$month_costs = 30; //Costs per month
$paid = 80; //Paid first & second month + 10 dollar / euro extra
$months = $costs / $month_costs; //Total months

//Substract amount paid from total costs
$remaining_costs = $costs - $paid;

//Get the modulo value - This will be how much is remaining for
//the first month that isn't fully paid yet
$mod = $remaining_costs % $month_costs;

//Get the remaining months
$remaining_months = $remaining_costs / $month_costs;

//Floor the remaining months
//So if $remaining_months = 2.354 etc, it becomes 2
$floored_months = floor($remaining_months);

//Substract the floored value from $months
$open_months = $months - $floored_months;

//Echo out what is paid
for($i=1;$i<$months;$i++){
    if($open_months > $i){
        echo "Month ". $i .": paid.<br />";
    } else {
        break;
    }
}

//Echo out last month paid if fully paid
if($open_months >= $months){
    echo "Month ". $i .": paid.<br />";
}

//Echo out the amount remaining for next month if there's still something open:
if($remaining_costs > 0 && $mod != 0){
    echo "Month ". $i .": $". $mod ." remaining.<br />"; //$mod will return 10 because 2 x 30 and 1 x 20 was paid
} else {
    echo "Month ". $i .": paid.<br />";
}

//Echo out the remaining months if there are any:
if($i < $months){

    //Update $i
    $i++;

    //Echo out the remaining months:
    for($y=$i;$y<=$months;$y++){
        echo "Month ". $y .": $". $month_costs ." remaining.<br />";
    }
}

//Echo out if there's to much paid:
if($paid > $costs){
    $toMuch = $paid - $costs;
    echo "You have paid ". $toMuch ." to much.";
}

?>

结果:

  

第1个月:已付款。
第2个月:已付款。
第3个月:剩余10美元   第4个月:剩余30美元。
第5个月:剩余30美元。第6个月:   剩余30美元。