drmticpet66231422 2016-02-18 10:19
浏览 51
已采纳

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

I got a project page that echoes multiple projects in a foreach loop, and every 4 results it starts a new row on the page.

However I got an element that always needs to be shown as the last result. How can I achieve that?

My foreach loop:

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

The element I want to be shown as last block:

<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 -->

This has the following result:

enter image description here

The element is now shown outside the row, while I need it to be inside the row (but only the last one).

  • 写回答

3条回答 默认 最新

  • duancong7573 2016-02-18 10:32
    关注

    Change your last if condition just after ending 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>';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示