foreach循环正在跳过3个项目中的前2个

时间:2015-07-16 18:56:45

标签: php mysql sql wordpress algorithm

我想知道你们是否可以帮我弄清楚为什么这不能正确生成HTML。变量$wholeTeam$wpdb->get_results(...)返回并确认长度为3(因为echo count($wholeTeam)正在吐出该数字,我直接在MySQL数据库中查看以确保有3行)。出于某种原因,只生成3个中的最后一个;前两个都不是。

    <h2>Current members <span class="title-count"> <?php echo count($wholeTeam) ?> </span></h2>
    <div class="row">
        <?php
        $sandwichTop = '<div class="col-xs-12 col-sm-6 col-md-4 col-lg-3"><img src="/assets/';
        $sandwichBottom = '/><button type="button" class="edit-mem-btn wp-core-ui button-primary">Edit</button></div>';
        foreach ($wholeTeam as $thisMember)
            $sandwichMiddle = $thisMember->picfn . '" id="memberid-' . $thisMember->id . '"';
            echo $sandwichTop . $sandwichMiddle . $sandwichBottom;
        ?>
    </div>  

生成为

<div class="row">
    <div class="col-xs-12 col-sm-6 col-md-4 col-lg-3">
          <img src="/assets/thepic.png" id="memberid-3"/>
          <button type="button" class="edit-mem-btn wp-core-ui button-primary">Edit</button></div>      
    </div>  
</div>

1 个答案:

答案 0 :(得分:2)

代码

foreach ($wholeTeam as $thisMember) {
    $sandwichMiddle = $thisMember->picfn . '" id="memberid-' . $thisMember->id . '"';
}
echo $sandwichTop . $sandwichMiddle . $sandwichBottom;

相当于:

foreach

这就是为什么你只回应一次。如果要回显每个foreach ($wholeTeam as $thisMember) { $sandwichMiddle = $thisMember->picfn . '" id="memberid-' . $thisMember->id . '"'; echo $sandwichTop . $sandwichMiddle . $sandwichBottom; } 次迭代:

Include