tags:

views:

40

answers:

3

Hi,

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

+4  A: 

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; ?>
thejh
I want the div to show underneath every record except the last one.
Kory
This is exactly what it does - you could also say that the div gets put over every record except the first one, it's basically the same.
thejh
Ahh.. gotcha, thanks.
Kory
Haha, clever. Putting the div _above_ the content.
kijin
It's a clever solution indeed. I have always had problems with that, especially while pulling the content out of a MySQL database (I had to execute `SELECT COUNT(*)` first). Actually I've never thought about putting the separator over the content, instead of putting it under the content. Thanks ;)
rhino
A: 
<?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

Ross
This won't work if another row has the same value as the last row.
willell
A: 

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

kijin
I don't understand this code... how can `++$count<$count` EVER return true? __EDIT__: Ahh, you have probably made a mistake. I guess you meant `++$current`, not `++$count`...
rhino
@rhino Yeah, that was a mistake. Fixed. Thanks.
kijin