2011-07-24 57 views
1

我正在創建一個網站,我需要在我的jQuery中使用一些PHP。PHP while循環與最後一條記錄?

我的代碼如下;

notifications.initMenu({ 
    2:'#chatPage #tabs #2', 
    3:'#chatPage #tabs #3', 
    4:'#chatPage #tabs #4', 
    5:'#chatPage #tabs #5' 
}); 

我想讓它使用while循環來回聲;

$pageID:'#chatPage #tabs #$pageID', 

然後在最後一個記錄上回顯;

$pageID:'#chatPage #tabs #$pageID' 

任何想法? 謝謝。

+1

向我們展示你已經有PHP代碼! – Shef

回答

7
$array = array(); 
while(....) { 
    $array[] = "\n $pageID:'#chatPage #tabs #$pageID'"; 
} 
echo implode(',', $array); 
+0

只是爲了好奇,哪一個更快,$ array []或array_push()? – Simon

+0

工作。非常感謝! – Oyed

+1

@Simon:顯然['$ array []'](http://stackoverflow.com/questions/1074059/array-push-vs-array-which-is-fastest)。 – Shef

1

如果逗號是你問什麼:

for ($i = 2; $i <= 5; $i++) { 
    echo "$i:'#chatPage #tabs #$i'" . ($i !== 5 ? "," ? "") . "\n"; 
}