我想插入動態月份到數據庫中,如果用戶選擇March,那麼我需要從3月到2月插入12個月的記錄。我獲得了動態的月份,但是當我試圖將其插入數據庫時,它只插入前12個月。如果用戶點擊添加更多按鈕,我需要在3月至2月再次重複循環。這是我的代碼:如何在循環中重複月份?
$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 into month(month_name) values('".$months[$i]."')";
}
其實,聽起來好像你的數據庫模型很奇怪。爲什麼你要插入所有十二個月,以一定的順序? 此外,months數組的長度不會超過12,因此直到23的循環將導致錯誤。 – Hidde