Find The Last Item In A Foreach
We've always tried to find ways to do pagination or correctly display lists. So, given that we use Yii to code in PHP, we'll get an array using $table = Table::model()->findAll("status='0'"); but never realized that you can use count(); to count how many rows came back. So here would be a solution to paging.
- $table = Table::model()->findAll("status='0' limit 10");
- $count = count($table);
- if($table){
- foreach($table as $key => $value){
- if($key==($count-1)){
- echo "last key";
- }
- }
- }
This is pretty cool, and makes pagination a lot easier. We used to do a query where we get the findAll and then we'd do a duplicate query where we do a count, which is totally a duplication of efforts and probably has increased a lot of processing time on page loads.