2016-11-10 12 views
1

私は自分のデータベースから値を取得するためにループしながら使用していますが、私の結果は次のようである:phpのwhileループで最後のカンマを文字列から削除しますか?

enter image description here

私は、ループの最後の値の後にコンマを削除します。

私はそれのためにも、SUBSTR関数をRTRIMを使用しますが削除しないでコンマ...のような

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

をコードを使用していました。

答えて

1

配列し、代わりに使用破でのポストのタイトルを取る..

<?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>' 
     } 
?> 
+0

ありがとう@prakashそれは今働いています。 – maddy

+0

ようこそ、plsは答えを受け入れる –

0

whileループでrtrimを削除しようとしましたが、ループした後にrtrim関数を作成しましたか?このように:

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

のようになりますトリム機能を持っているはい、私はそれを試してみましたが、それは私だけループの最初の値を与えますlike ... Aprilia ドルソドゥーロ750 ABS、 PRI PRIMODE、 ヤマハ XS650、 ゼロ S、 – maddy

+0

ああ、文字列には、すべての$ post-> post_titleを連結して連結する必要があります。もう一度この時間をお試しください。 –

3

そのためRTRIMを活用します。文字列スペースの最後には、コンマの後には置かないでください。

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

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

または

$str = rtrim($str,","); 
試します

...

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

PHPは

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

は出力がこの

item one ,item tow,item three,item 4 
関連する問題