2013-02-21 48 views
0

我想重提我原來的問題:Populating a calendar with PHP foreach code的foreach問題與空變量

本地主機服務器上的工作時,腳本工作,但上傳到網上時,日曆不會出現,和Chrome給人我以下錯誤:

<b>Warning</b>: array_values() [<a href='function.array-values'>function.array-values</a>]: The argument should be an array in <b>/home/flyeurov/public_html/lib/skins/flyeuro/events/events_index.tpl</b> on line <b>30</b> 
<b>Warning</b>: array_merge() [<a href='function.array-merge'>function.array-merge</a>]: Argument #2 is not an array in <b>/home/flyeurov/public_html/lib/skins/flyeuro/events/events_index.tpl</b> on line <b>30</b> 
<b>Warning</b>: Invalid argument supplied for foreach() in <b>/home/flyeurov/public_html/lib/skins/flyeuro/events/events_index.tpl</b> on line <b>30</b><br /> 

我發現,這種情況正在發生時,我沒有任何事件在過去發生的事情= $history因此空,或計劃在未來事件= $events空。

foreach(array_merge(array_values($history), array_values($events)) as $event) 

但是我的系統有時約束,不得有任何活動計劃,因此空$event,所以我的問題是,我怎麼能繞過的foreach與一個或另一個空變量反正顯示日曆?

+0

只是加入合併另一個參數:'new array()' – Dharman 2013-02-21 12:35:34

+1

如果過去沒有事件發生,$ history應該是一個**空數組**,而不是**空變量** 。正確初始化你的變量,你會沒事的。 – 2013-02-21 12:39:28

回答

2
if(!empty($history) || !empty($events)){ 
    foreach(array_merge(array_values($history), array_values($events)) as $event){...} 
} 

在你的情況這將是一個更好的解決方案(刪除了如果條件):

$history = (!empty($history))?$history:array(); 
$events = (!empty($events))?$events:array(); 
foreach(array_merge(array_values($history), array_values($events)) as $event){...} 
+0

現在,日曆已經消失。 – zzwyb89 2013-02-21 12:40:40

+0

你在foreach循環中究竟做了什麼? – 2013-02-21 12:42:42

+0

http://pastebin.com/0f5sipJu - 結果應該是即使變量爲空也應該顯示日曆,但它們當前有事件,但仍未出現。我一定在這段代碼中搞砸了一些東西。 Chrome正在拋出'Syntax Error:unexpected token:['on'eventdates']。 – zzwyb89 2013-02-21 12:45:48

0

如果{ ... }

(空($陣列)!)這將在foreach之前進行檢查。