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'];
    }
}

1 Answer 1

2

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

echo implode(", ", $page_tags);
Sign up to request clarification or add additional context in comments.

4 Comments

<pedantry> echo implode(", ", $page_tags); </pedantry>
@fredley excuse my ignorance what is <pedantry>?
@askIT yes. @fredley is referring to his suggesting a minor change (the additional space after the comma)
@askIT it's worth looking into similar tags, <irony> and <sarcasm> in particular, they can be very useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.