2011-08-06 101 views
0

我正在使用循環從數據庫中獲取記錄。我想要它,所以在每條記錄之後都有一個逗號,除非它是最後一條記錄,或者只有一條記錄要顯示。帶有逗號的單獨條目,除最後一項外?

這裏是我的部分代碼:

while($medals = $db->fetch_array($query)) 
{ 
    if($medals['post_count'] != "") 
    { 
     $requirements = $medals['post_count']." posts"; 
    } 
    if($medals['time_registered'] != "") 
    { 
     $requirements .= "Registered for ".$medals['time_registered']." days"; 
    } 
    if($medals['thread_count'] != "") 
    { 
     $requirements .= $medals['thread_count']." threads"; 
    }   
    if($medals['num_reps'] != "") 
    { 
     $requirements .= $medals['num_reps']." positive reputations"; 
    } 
} 

我怎樣才能做到這一點?

謝謝。

回答

1
while($medals = $db->fetch_array($query)) 
{ 
    $requirements = array(); 
    if($medals['post_count'] != "") 
    { 
     $requirements[] = $medals['post_count']." posts"; 
    } 
    if($medals['time_registered'] != "") 
    { 
     $requirements[] = "Registered for ".$medals['time_registered']." days"; 
    } 
    if($medals['thread_count'] != "") 
    { 
     $requirements[] = $medals['thread_count']." threads"; 
    }   
    if($medals['num_reps'] != "") 
    { 
     $requirements[] = $medals['num_reps']." positive reputations"; 
    } 
    $requirements = implode(', ',$requirements); 
} 

或者你需要逗號之間的行嗎?

相關問題