1

I am using while loop to get values from my database and my result is like:

enter image description here

I want to remove comma after last value of loop.

i used code like..

<?php
 $cat = get_terms('car_category'); // you can put your custom taxonomy name as place of category.
        foreach ($cat as $catVal) {
            echo '<b>'.$catVal->name.'</b>';
            $postArg = array('post_type'=>'cars','posts_per_page'=>5,'order'=>'desc',
                              'tax_query' => array(
                                                    array(
                                                        'taxonomy' => 'car_category',
                                                        'field' => 'term_id',
                                                        'terms' => $catVal->term_id
                                                    )
                            ));

            $getPost = new wp_query($postArg);
            global $post;

            if($getPost->have_posts()){
                $str = "";
                echo '<div class="yearDiv">';
                    while ( $getPost->have_posts()):$getPost->the_post();
                        $str =rtrim($post->post_title, ",");
                        echo '<span>'.$str.',</span>';
                    endwhile;
                echo '</div>';
            }
        }
?>

i used rtrim , substr function also for it but comma not removing.

5 Answers 5

3

Utilize rtrim for it. Just make sure at the end of the string space should not be there after comma.

   $yourString = rtrim($yourString, ",");
Sign up to request clarification or add additional context in comments.

Comments

1

Take the post titles in an array and Use Implode instead ..

<?php
 $cat = get_terms('car_category'); // you can put your custom taxonomy name as place of category.
        foreach ($cat as $catVal) {
            echo '<b>'.$catVal->name.'</b>';
            $postArg = array('post_type'=>'cars','posts_per_page'=>5,'order'=>'desc',
                              'tax_query' => array(
                                                    array(
                                                        'taxonomy' => 'car_category',
                                                        'field' => 'term_id',
                                                        'terms' => $catVal->term_id
                                                    )
                            ));

            $getPost = new wp_query($postArg);
            global $post;

            if($getPost->have_posts()){
                $str = array();

                    while ( $getPost->have_posts()):$getPost->the_post();
                        $str[] = $post->post_title;

                    endwhile;
            }
            echo '<div class="yearDiv">';
            echo '<span>'.implode(', ', $str).'</span>';
            echo '</div>'
        }
?>

Comments

0

Have you tried removing the rtrim on the while loop but instead after looping you make the rtrim function? Like this:

echo '<div class="yearDiv">';
    while ( $getPost->have_posts()):$getPost->the_post();
        $str .= $post->post_title . ",";
    endwhile;
    echo '<span>'.rtrim($str, ",").'</span>';
echo '</div>';

2 Comments

Yes i tried it but it only giving me first value of loop like.. Aprilia Dorsoduro 750 ABS, PRI PRIMODE, Yamaha XS650, Zero S,
Ah yes, you have to use concat for the strings.. glueing all your $post->post_title.. try again this time.
0
echo '<div class="yearDiv">';
$str = "";
while ( $getPost->have_posts()):$getPost->the_post();
    $str .= $post->post_title.",";
endwhile;

try

if(substr($str, -1) == ',') {
   $str = substr($str, 0, -1);
}

or

$str = rtrim($str,",");

...

echo '<span>'.$str.'</span>';
echo '</div>';

Comments

0

Php has a trim function

<?php
$str = "item one ,item tow,item three,item 4,";
echo trim($str, ",");
?>

the output will be like this

item one ,item tow,item three,item 4

1 Comment

No, the output will be like this "item one item tow item three item 4 " because it will remove the comma from every iteration of the loop

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.