如何在PHP结果表

时间:2018-05-22 07:14:10

标签: php

如何使用PHP在搜索结果表中添加逗号?下面是我得到的代码。结果在第二列中没有逗号的情况下粘在一起,我尝试添加逗号,例如echo $aaa = $newlist->name.',';。它没有用。

table

<tr>
    <th>Class Name </th>
    <th>Class Type</th>
    <th>Recommended Fitness Level</th>
    <th>Location</th>
</tr>
<?php
for ($i = 0; $i < $countposts; $i++) {

    $pid = $unique_posts[$i];
    $title = get_the_title($pid);
    $location = get_field('location', $pid);
    $fitness_level = get_field('fitness_level', $pid);
    $imp_fitness = implode(',', $fitness_level);
    include("locationfile.php");
    ?>
    <tr>
        <td><?php echo $title; ?></td>
        <td><?php
            $term_list = wp_get_post_terms($pid, 'classtypecategory', array("fields" => "all"));
            foreach ($term_list as $newlist) {
                echo $aaa = $newlist->name . '';
            }
            ?>
        </td>
        <td><?php echo $imp_fitness; ?></td>                   
        <td><a href="<?php echo $url; ?>" target="_blank"><?php echo $location; ?></a></td>
    </tr>
<? } ?>

1 个答案:

答案 0 :(得分:-1)

//Method 1:
foreach($term_list as $newlist){
$aaa   .=  $newlist->name.','; 
} 
echo $aaa=substr($aaa,0,-1);

//Method 2:
// For implode
foreach($term_list as $newlist){
$text[] =  $newlist->name; 
} 
echo $aaa=implode(',',$text);

试试这个,而不是你的foreach