始终在每个循环的结尾处显示特定结果

时间:2016-02-18 10:19:52

标签: php for-loop

我有一个项目页面,它在foreach循环中回显多个项目,每4个结果就会在页面上开始一个新行。

但是我得到的元素总是需要显示为最后的结果。我怎样才能做到这一点?

我的foreach循环:

<? 
$tel = 1;

foreach ($projectcr as $project) {
$article_images = $project['images']; // Get image parameters of the article

$pictures = json_decode($article_images); // Split the parameters apart
$introtext = $project['introtext'];
    if ($project['title'] != '') {
        if($tel == 1) {
            echo '<div class="row">';   
        }
        echo '
        <div class="col-xs-12 col-sm-6 col-lg-3 js-wpg-item" data-categories="gardening">
            <a class="card  portfolio-grid__card js-wpg-card" href="project/'.$project['alias'].'.html">
                <img src="cms/'.$pictures->{'image_intro'}.'" class="card-img-top portfolio-grid__card-img wp-post-image" alt="19" height="240" width="360">
                <div class="card-block portfolio-grid__card-block">
                    <h5>'.$project['title'].'</h5>
                    <p>'.$project['created'].'</p>
                </div>
            </a>
        </div>'
            ;

        if(($tel % 4) == 0){
            echo '</div> <div class="row">'; 
        }
        $tel++; 
    }
} 
if(($tel % 4) != 0){
    echo '</div>';
}
?>

我想要显示为最后一个块的元素:

<div class="row">
    <div class="col-xs-12 col-sm-6 col-lg-3 js-wpg-item" data-categories="">
        <div class="portfolio-grid__card portfolio-grid__card--dummy js-wpg-card" href="projects/">
            <div class="portfolio-grid__card-block text-center">
                <span class="fa fa-cloud-upload fa-2x"></span>
                <h5>Uw volgende project hier?</h5>
                <p class="portfolio-grid__card-text">
                    <a href="contact.php" class="btn btn-primary">NEEM CONTACT OP</a>
                </p>
            </div>
        </div>
    </div><!-- /.col -->

</div><!-- /.row -->

这具有以下结果:

enter image description here

元素现在显示在行外面,而我需要它在行内(但只有最后一行)。

3 个答案:

答案 0 :(得分:1)

在结束if condition

后立即更改上一个foreach
if(($tel % 4) != 0){
    //echo div.col-... inside .row started in loop
    echo '<div class="col-xs-12 col-sm-6 col-lg-3">always to be shown at last</div>';
    //end .row
    echo '</div>';
}

答案 1 :(得分:0)

有多种方法可以实现这一目标,但是您只是在阵列中添加元素吗?

$p = [];
$p['title'] = 'Uw volgende project hier?';
$p['created'] = '<a href="contact.php" class="btn btn-primary">NEEM CONTACT OP</a>'

array_push($projectcr, $p);

另一种方法是检查它是否是数组中的最后一个元素

$lastElement = end($array);
// you code 
    if($project == $lastElement){
        //add the static block
    }

答案 2 :(得分:0)

在循环结束时添加要显示的内容。不要检查条件并为行div添加结束标记,因为如果$tel的模数等于零,它将无法添加关闭标记。您的上一个内容应该显示在新行中,但不会关闭标记。

$last = <div class="col-xs-12 col-sm-6 col-lg-3 js-wpg-item" data-categories="">
    <div class="portfolio-grid__card portfolio-grid__card--dummy js-wpg-card" href="projects/">
        <div class="portfolio-grid__card-block text-center">
            <span class="fa fa-cloud-upload fa-2x"></span>
            <h5>Uw volgende project hier?</h5>
            <p class="portfolio-grid__card-text">
                <a href="contact.php" class="btn btn-primary">NEEM CONTACT OP</a>
            </p>
        </div>
    </div>
</div>

foreach($projectcr as $project) {
...//your code
...
}

echo $last. '</div>';