0
我有一個SQL查詢,返回:未定義偏移1在我的PHP代碼 - 如何避免它?
start_time of the event | end time of the event | duration of the event
在我的PHP代碼我遍歷結果,我檢查,如果我可以插入現有的另一個活動 - 基本上我檢查,如果一個事件結束和前一個事件開始之間的時間差異比前一個事件的持續時間長 - 如果是,那麼我將其結束日期保存爲新創建事件的開始日期。
我創建一個PHP查詢發生之後我做的SQL SELECT查詢:
if($q3->rowCount()>0)
{
$check3 = $q3->fetchAll(PDO::FETCH_ASSOC);
$arr = array();
foreach ($check3 as $row) {
$arr[] = $row;
}
$firstRow = $arr[0];
for($i=0; $i < count($arr); $i++) {
$current_row = $arr[$i];
$next_row = $arr[$i+1];
if (($next_row["begin_date"] - $current_row["end_date"]) >= $duration){
$found_timestamp_for_new_event = $current_row["end_date"];
break;
}
}
$last_element = ($arr[count($arr) - 1]['end_date']);
if($end_timestamp - $last_element >= $duration){
$found_timestamp_for_new_event = $last_element;
}
}
然而,當我運行它,我得到了警告:
Notice Undefined offset: 1 in ...
和警告在$next_row = $arr[$i+1];
。 我認爲這是因爲在某些情況下查詢不會返回任何事件,因此它不會看到$arr[$i+1]
。
我該如何解決?