2010-10-31 62 views
1

如何實現下面不想顯示最後結果的部分?檢查PHP中的最後一條記錄

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

感謝

回答

5

也許這樣嗎?

<?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; ?> 
+0

我想div顯示在除了最後一個記錄以外的每個記錄下 – Kory 2010-10-31 21:41:35

+0

這正是它所做的 - 你也可以說div除了第一個記錄,它基本上是一樣的 – thejh 2010-10-31 21:45:13

+0

Ahh .. gotcha,thanks。 – Kory 2010-10-31 21:48:03

0
<?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; ?> 

應該這樣做

+0

這不會工作,如果另一行與最後一行具有相同的值。 – 2010-10-31 21:44:07

0

另一種方式來做到這一點:

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

從技術上講,像分隔應該用CSS來實現,使用:first-child:last-child僞類。但IE不支持:(

+0

我不明白這個代碼...如何可以'++ $ count <$ count' EVER返回true? __EDIT__:啊,你可能犯了一個錯誤。我想你的意思是'++ $ current',而不是'++ $ count' ... – rhino 2010-10-31 21:51:31

+0

@rhino是的,那是一個錯誤。固定。謝謝。 – kijin 2010-10-31 22:29:52