0

i have some checkboxes which created by For loop like this:

for ($i=1; $i <= $loan['loanreqmonth']; $i++)
{
    echo '<input name="loan['.$i.']" type="checkbox" value="'.$i.'">loan # '.$i.'</div>';
}

and i process and import the selected checkboxes whith something like this:

foreach($_POST['loan'] as $loan)
{
    mysql_query("INSERT INTO `loanpays`(`loanid`,`type`,`num`,`codemelli`, `price`, `month`, `year`,`stamp`) VALUES ('$loan[ID]','loan','$loan','$_SESSION[codemelli]','$priceloan','$month','$year','$stamp')");
}

i want to put month (1-12) and year(2017-2020) from current month and year for each selected checkbox. for example the first checkbox month is (11) and year is(2017) the next one is 12 / 2017 but the problem is the next one is 1 / 2018!

how can I solve this problem?

mega6382
  • 9,211
  • 17
  • 48
  • 69

1 Answers1

0

If I understand the question correctly:

Look at modulo operator (%) it gives you a remainer after division by the specified number - lets say 2 % 12 = 2, 13 % 12 = 1 - this will allow you to loop through months 1 to 12 infinitely, you just need to take care of the 12 month because 12 % 12 will give you 0 where you need 12.

Too iterate the year, simply check whether the current loop month value is divisable by 12 (x % 12 == 0) and if so iterate the year at the end of loop, cause next month will be January of next year...

  • thanks for your useful guide! i have $loanmonth=18; and it is the number of payments which user must pay for this loan.i want the first loan date to be current month and year(eg. 2017/11) till 18 months ( in this case 2019/4),is it possible? – Ali Khedmati Nov 14 '17 at 10:30
  • then just dont start the loop from 1 (`for($i=1;...`), but from 11 (`for($i=11;...`) – David Bambušek Nov 14 '17 at 10:37
  • i wrote this and its successfully works for returning months but how can i write something for years? – Ali Khedmati Nov 14 '17 at 11:04
  • for ($i=1; $i <= $loanreqmonth; $i++){ if($i%12 == 0){ $b=12; } elseif($i%12 != 0){ $b=$i%12; } echo'loan # '.$b.' ';} – Ali Khedmati Nov 14 '17 at 11:05
  • as you can see i use if clause for the month 12 and i dont know how to iterate year ! – Ali Khedmati Nov 14 '17 at 11:06
  • before the loop, declare a variable and give it value of the starting year:`$year = `, then add a condition to the last line of the loop to iterate the year if you reached last month of the year `if($i % 12 == 0){$year++}` – David Bambušek Nov 14 '17 at 11:15
  • i did it but for the month no 12 of every year;the `{$year++}` runs! – Ali Khedmati Nov 14 '17 at 11:59
  • So everything is working as it should be now? If so, I was glad to help! – David Bambušek Nov 14 '17 at 12:23
  • there is a problem! for example the first checkbox month is 10 and year is 2017 , next one is 11/2017 and the next is 12/2018 ! i have problem with month 12 of every year.here is the code: `for ($i=1; $i <= $loanreqmonth; $i++){ if($i%12 == 0){ $b=12; } elseif($i%12 != 0){ $b=$i%12; } if($i%12 == 0){$years++;}}` – Ali Khedmati Nov 14 '17 at 12:56
  • you need to put the `if($i%12 == 0){$years++;}` after your `echo – David Bambušek Nov 14 '17 at 13:14
  • Thank you very much! You completely solved my problem,Thanks a lot buddy ♥ – Ali Khedmati Nov 14 '17 at 13:27