Page 1 of 1

PHP + SQL: Results seperated by comma

Posted: Tue Apr 27, 2010 4:17 pm
by bla5e
Noob question, but how do I stop a comma from appearing after the last result?

Code: Select all

echo '<i>Likes: </i>';
    while ($rows = pg_fetch_assoc($like)){
      echo $rows['author'];
      echo ', ';
    }
    echo '<br><i>Dislikes: </i>';
    while ($row = pg_fetch_assoc($dislike)) {
      echo $row['author'];
      echo ', ';
    }
thanks in advance.

Re: PHP + SQL: Results seperated by comma

Posted: Tue Apr 27, 2010 4:31 pm
by John Cartwright
I tend to store my results in an array, which makes it simple to create comma seperated output, i.e.,

Code: Select all

$likes = array();
while ($rows = pg_fetch_assoc($like)){
   $likes[] = $rows['author'];
}

echo 'Likes: '. implode(', ', $likes); 

Re: PHP + SQL: Results seperated by comma

Posted: Tue Apr 27, 2010 4:46 pm
by bla5e
ty awesome idea!