开发者

PHP separate entries with a comma, except last entry?

开发者 https://www.devze.com 2023-03-26 01:59 出处:网络
I\'m using a loop to fetch records from a database. I want it so there is a comma after each record, unless it\'s the last record, or if there is only one record to display.

I'm using a loop to fetch records from a database. I want it so there is a comma after each record, unless it's the last record, or if there is only one record to display.

Here's my partial code:

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";
    }
}

How can I acc开发者_开发知识库omplish this?

Thanks.


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);
}

Or you need commas between rows?

0

精彩评论

暂无评论...
验证码 换一张
取 消