tags:

views:

34

answers:

1

I want to be able to display my posts tags array [0] => grill [1] => meat [2] => hot-dogs with a comma separating each tag and was wondering how can I do this using PHP?

Stored in the database.

$page_tags = Array ( [0] => grill [1] => meat [2] => hot-dogs ) 

Desired output.

grill, meat, hot-dogs

Here is the PHP & MySQL code.

$page_tags = array();
$dbc = mysqli_query($mysqli,"SELECT tags.*, posts_tags.* 
                             FROM tags 
                             INNER JOIN posts_tags ON tags.id = posts_tags.tag_id
                             WHERE posts_tags.post_id= '" . $pageID . "'
                             AND posts_tags.user_id = '" . $userID . "'
                             GROUP BY tags.tag");

if (!$dbc) {
    print mysqli_error($mysqli);
} else {
    while($row = mysqli_fetch_array($dbc)){
        $page_tags[] = $row['tag'];
    }
}
+2  A: 

Seeing as you have an array $page_tags, you can use implode() to output it separated by commas:

echo implode(", ", $page_tags);
Pekka
`<pedantry> echo implode(", ", $page_tags); </pedantry>`
fredley
@fredley good point! Added.
Pekka
@fredley excuse my ignorance what is `<pedantry>`?
askIT
@askIT http://en.wikipedia.org/wiki/Pedant
Pekka
A Negative word according to the Wiki or am I wrong?
askIT
@askIT yes. @fredley is referring to his suggesting a minor change (the additional space after the comma)
Pekka
@askIT it's worth looking into similar tags, `<irony>` and `<sarcasm>` in particular, they can be very useful.
fredley