So I know this is simple but I’ve been banging my head against the wall for a while trying to figure it out. I want to show a ruler at the bottom of my loop on each one except the last one. I can get it to work if I have the exact number of records but not if I have less. For example if the max number to show is 10 but there are only 5 records I want the divider after the 4th record. Likewise, if there are 20 results but max is 10 I want it after the 9th.
<?php $subscriberIDs = ba_getUsersByRole( 'subscriber' );
// Loop through each user
$i=0;
$max = 10; //max number of results
$total_users =count($subscriberIDs); //total number of records
foreach($subscriberIDs as $user) :
if($i<=$max) : ?>
<li>
<?echo $user['data'];?>
</li>
<?php
if(($i < $total_user-1 && $max >= $total_users) || ($i < max-1 && $total_users <= $max)){echo "<hr>";}
$i++;
endif;
endforeach; ?
This takes the lesser of either
$max-1orcount($subscriberIDs)-1, which by definition is will be the last item you’ll iterate over. If you have more than$maxitems, then this will be$max-1, if you have less than$maxitems, then this will becount(.)-1.Then, during the iteration, the
ifstatement prints an<hr>so long as the current item is not the last item.