没有得到正确的foreach

时间:2017-03-26 22:43:36

标签: php while-loop explode

请问我有一个问题,即在一个while循环中运行foreach,我有时只获得该列中的第一个或最后一个结果。

while ($girl = mysqli_fetch_array($obo)) {
    $ch_desc = $girl['desc'];
    $ch_tags = $girl['tags'];  //eg boy,ope,not,good

    $laye = explode (",", $ch_tags);
        foreach ($laye as $fb){
            $yepa = "<span> #$fb </span>";  
        }

    <div class="panel-heading">
        <?php echo $yepa ?> 
    </div>
}

1 个答案:

答案 0 :(得分:0)

您正在覆盖每个循环中变量的值。您需要将其初始化为空字符串并在每个循环中连接标记。

$tags = explode (",", $ch_tags);
$tagList = "";
foreach ($tags as $tag){
    $tagList .= "<span> #$tag </span>";
}