<?php $post_ids = $wpdb->get_col("SELECT comment_post_ID FROM $wpdb->comments WHERE user_id = $current_user->ID AND comment_approved = '1' "); foreach($post_ids as $post_id) { $shedeg .= "$post_id, "; } echo $shedeg; ?> PHP: As you see this code adds , symbol to every $post_id value. How can I prevent adding it to the last value only. Now the result is: 1, 2, 3, 4, 5, And I need it to be: 1, 2, 3, 4, 5 Thanks in advance
<?php $post_ids = $wpdb->get_col("SELECT comment_post_ID FROM $wpdb->comments WHERE user_id = $current_user->ID AND comment_approved = '1' "); foreach($post_ids as $post_id) { $shedeg .= "$post_id, "; } $shedeg = substr($shedeg, 0, -1); echo $shedeg; ?> add new line (bolded) rep me up
I would do that like this <?php $post_ids = $wpdb->get_col("SELECT comment_post_ID FROM $wpdb->comments WHERE user_id = $current_user->ID AND comment_approved = '1' "); $shedeg=''; foreach($post_ids as $post_id) { $shedeg .= (('' === $shedeg)?', ':'').$post_id; } echo $shedeg; ?> PHP: PS I believe $shedeg = trim($shedeg, ","); won't work since the last char is ' ' (space) not comma.
Guys, there's a function specific to this: $shedeg = implode(',', $shedeg); Code (markup): There, no need to complicate matters