1
每當我嘗試運行這段代碼時,它就好像進入了一個無限循環。但是,我不知道是什麼導致這個問題。也許一個額外的眼睛就可以指出這個問題?在PHP中無限循環
的問題時,纔會有不同的年份區域,例如2012年至2018年
例accours函數:$ this-> budget_model-> set_monthly_budget( '1', '2012', '8', '2014' , '1');
function set_monthly_budget($start_month, $start_year, $end_month, $end_year, $budget_group_id)
{
$user_id = 2;
// Current date
$current_month = $start_month;
$current_year = $start_year;
$days_in_current_month = cal_days_in_month(CAL_GREGORIAN, $current_month, $current_year);
$company_id = 1;
$month_goal = 100;
// Check if it is the current month
if($start_year == $end_year)
{
for($x=$current_month;$x<=$end_month;$x++)
{
$data = array(
'user_id' => $user_id,
'budget_group_id' => $budget_group_id,
'month' => $x,
'year' => $start_year,
'company_id' => $company_id,
'month_goal' => $month_goal
);
// Inserting information into the database
$this->db->insert('budget_month',$data);
}
return true; // Return true if the task was completed
}
if($start_year !== $end_year)
{
$temp_start_year = $start_year;
while($temp_start_year !== $end_year)
{
// Check if we are in the same year as we started
if($temp_start_year == $current_year)
{
// Insert remaining months for this year
for($x=$current_month;$x<=12;$x++)
{
$data = array(
'user_id' => $user_id,
'budget_group_id' => $budget_group_id,
'month' => $x,
'year' => $temp_start_year,
'company_id' => $company_id,
'month_goal' => $month_goal
);
// Inserting information into the database
$this->db->insert('budget_month',$data);
}
}
// Check if the temp and end year is the same
if($temp_start_year < $end_year)
{
// Insert remaining months for this year
for($x=1;$x<=12;$x++)
{
$data = array(
'user_id' => $user_id,
'budget_group_id' => $budget_group_id,
'month' => $x,
'year' => $temp_start_year,
'company_id' => $company_id,
'month_goal' => $month_goal
);
// Inserting information into the database
$this->db->insert('budget_month',$data);
}
}
// Check if we are in the same year as we started
if($temp_start_year == $end_year)
{
// Insert remaining months for this year
for($x=1;$x<=$end_month;$x++)
{
$data = array(
'user_id' => $user_id,
'budget_group_id' => $budget_group_id,
'month' => $x,
'year' => $temp_start_year,
'company_id' => $company_id,
'month_goal' => $month_goal
);
// Inserting information into the database
$this->db->insert('budget_month',$data);
}
}
$temp_start_year++;
}
}
}
如果'$ start_year'大於'$ end_year',則while循環的條件將始終得到滿足,從而導致無限循環。 – Vulcan
上面的代碼有兩個循環。使用一些'echo'語句來縮小這兩個循環中哪一個沒有終止。 – xbonez