php - How to repeat months in loop ? -


i want insert dynamic months database if user select march need insert record 12 months march feburary. getting dynamic months when trying insert database insert first 12 months. need repeat loop again march february if user click on add more button. code :

$months = array(); $date="august"; $year= '2014'; //$y= (int)$year; $currentmonth= date('m', strtotime($date)); $currentyear= date('y', strtotime('+1 year')); for($x = $currentmonth; $x < $currentmonth+12; $x++)  {      $months[] = date('f y', mktime(0, 0, $currentyear, $x,1)); } //print_r($months); for($i=0; $i<=23 ; $i++) {   echo $insert= "insert month(month_name) values('".$months[$i]."')";  } 

as months array has 12 values, can't go value 23 in array. can run through array twice 0 11, this:

for($j=0; $j<2 ; $j++) {   for($i=0; $i<12 ; $i++)   {     echo $insert= "insert month(month_name) values('".$months[$i]."')";   } } 

or clyde indicated can use modulo operator, doesn't make waste 2 loops , faster:

  for($i=0; $i<24 ; $i++)   {     echo $insert= "insert month(month_name) values('".$months[$i % 12]."')";   } 

Comments

Popular posts from this blog

How to logout from a login page in asp.net -

Stack level too deep error after upgrade to rails 3.2 and ruby 1.9.3 -