首页 > 解决方案 > 如何删除最后一个


在 Codeigniter 的 foreach 循环中标记?

问题描述

很抱歉发布这样的帖子,但我正在学习我希望所有结果都有一个 hr 或任何基于 html 的行,除了最后一个我放了一个换行符,但它看起来对我有帮助

foreach ($newsqry->result() as $n) {
    $nr=$n->numrows();
    $nlink = array_keys($routesAll, "news/index/" . $n->n_id);
    ?>
            <div class="row">
            <div class="col-md-6 imgpost">
                            <a class="post-imgn" href="<?=site_url($nlink)?>">
                                <img  class=""src="<?=base_url("assets/uploads/news/thmb/$n->img")?>"alt="<?=character_limiter($n->title,'50')?>">
                            </a>
                        </div>
                            <div class="col-md-6 descpost">
                                <div class="post-category com" >
             <?php
    $t = $n->tags;
    $s = explode(',', $t);
    foreach ($s as $a) {
        ?>
                            <a class="tags" href="<?=site_url("news/view?tag=".strtolower(str_replace(' ','-',$a)))?>"><?=ucwords($a)?></a>
                        <?php } ?>
            </div>
                                <h3 class="post-title news">
                                    <a href="<?=site_url($nlink)?>"><?=character_limiter($n->title,'100')?>"</a>
                                </h3>
                                <ul class="post-meta">
                                    <li><a href="<?=site_url("news/view?author=".strtolower(str_replace(' ','-',$n->author)))?>"><?=$n->author?></a></li>
                                    <li><i class="fa fa-clock-o" aria-hidden="true"></i> <?=date("h:i A",strtotime($n->time))?> <?=date("F-d-y",strtotime($n->date))?></li>
                                </ul>
                                <p class="descfornews"><?=character_limiter(strip_tags($n->desc),'190')?></p>
                            </div>
                        </div>
                        <?php if($nr<1){echo "<hr>";}else{ echo "<h3>End of Page</h3>";}?>
                <?php
}
?>

标签: phphtmlcodeigniter

解决方案


I think you can do something like this. it will print


before the data printed, but there is $data variable to prevent it to print before the first data.

 <?php
        $data = 0;
        $t = $n->tags;
        $s = explode(',', $t);
        foreach ($s as $a) {
          if($data!=0){echo "<hr>";}
            ?>
            <a class="tags" href="<?=site_url("news/view?tag=".strtolower(str_replace(' ','-',$a)))?>"><?=ucwords($a)?></a>
        </div>
            <h3 class="post-title news"><a href="<?=site_url($nlink)?>"><?=character_limiter($n->title,'100')?>"</a></h3>
                  <ul class="post-meta">
                        <li>
                            <a
                                href="<?=site_url("news/view?author=".strtolower(str_replace(' ','-',$n->author)))?>"><?=$n->author?></a>
                        </li>
                        <li>
                      <i class="fa fa-clock-o" aria-hidden="true"></i>
                      <?=date("h:i A",strtotime($n->time))?>
                      <?=date("F-d-y",strtotime($n->date))?></li>
                  </ul>
            <p class="descfornews"><?=character_limiter(strip_tags($n->desc),'190')?></p>
        </div>
        </div>
        
    <?php
    }
        echo("<h3>End of Page</h3>");
    ?>

推荐阅读