开发者

Checking for last record in PHP

How can I implement the part below that I do not want to display on the last result?

<?php foreach ($products->result() as $row): ?>
    <h3><?= $row->header; ?></h3>
    <p><?= $row->teaser; ?> </p>
    <a href="">Read More</a>    
    <!-- DONT DISPLAY THIS LAST LINE IF ITS THE LAST RECORD -->
    <div class="divider"></div>   
 <?php endforeach;开发者_运维技巧 ?>

Thanks


Maybe like this?

<?php $firstline=true; foreach ($products->result() as $row): ?>
    <?php if ($firstline) {
        $firstline=false;
    } else {
        echo '<div class="divider"></div>';
    }?>
    <h3><?= $row->header; ?></h3>
    <p><?= $row->teaser; ?> </p>
    <a href="">Read More</a>
<?php endforeach; ?>


<?php foreach ($products->result() as $row): ?>
    <h3><?= $row->header; ?></h3>
    <p><?= $row->teaser; ?> </p>
    <a href="">Read More</a>    

    <?php if($row!=end($products->result())
    <!-- DONT DISPLAY THIS LAST LINE IF ITS THE LAST RECORD -->
    <div class="divider"></div> 
    <?php } ?>  

 <?php endforeach; ?>

should do it


Another way to do it:

<?php $results = $products->result();
      $count = count($results);
      $current = 0;
      foreach ($results as $row): ?>

    <h3><?= $row->header; ?></h3>
    <p><?= $row->teaser; ?> </p>
    <a href="">Read More</a>

    <?php if (++$current < $count): ?>
        <div class="divider"></div>   
    <?php endif; ?>

<?php endforeach; ?>

Technically, things like dividers should be done with CSS, using the :first-child and :last-child pseudo-classes. But IE doesn't support it :(

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜