0

I am wanting to separate these links that are outputted by a comma, but with no comma on the last link. What is the best way to achieve this?

<p class="favs">Favourite Courses: 
                <?php 
                    $user_id = bp_get_member_user_id();
                    $array = get_user_meta($user_id, 'wpfp_favorites', true); 

                    foreach ($array as $key => $value) {
                    $queried_post = get_post($value); ?>

                    <a href="http://publicaccessgolf.com.au/<?php echo $queried_post->post_name; ?>" title="<?php echo $queried_post->post_title; ?>" ><?php echo $queried_post->post_title; ?></a>

                 <?php } ?>
                </p> 

1 Answer 1

4

The best way is typically this:

$links = array();
foreach (...) {
    $links[] = '<a ...';
}

echo join(', ', $links);

Adapt to your situation as appropriate.

Sign up to request clarification or add additional context in comments.

5 Comments

implode = join, just saying. :)
That is not working. I am taking the values (post id) from the array and using these values to get the post URL and title. So I need to echo out the URL and title, not the values from the array. If that makes sense? (probably not :)
@user Ever heard of string concatenation? "string" . $variable . "string"
@deceze I have heard of string concatenation. How does that help what I am trying to do though?
Take a good look again at my answer. Instead of echoing out all the links and titles directly, put them into an array $links first. You make a new array. When you have an array full of such links and titles, output them using join, which puts in the right number of commas.

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.