开发者

show a divider at bottom of loop except for last item

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


// <hr> goes in every spot, but not on the last item, up to 10
$position = min($max-1, count($subscriberIDs)-1);
$i = 0;
foreach($subscriberIDs as $user){
   echo '<li>' . $user['data'] . '</li>';
   if($i != $position){
       echo '<hr>';
   }
   $i++;
}

This takes the lesser of either $max-1 or count($subscriberIDs)-1, which by definition is will be the last item you'll iterate over. If you have more than $max items, then this will be $max-1, if you have less than $max items, then this will be count(.)-1.

Then, during the iteration, the if statement prints an <hr> so long as the current item is not the last item.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜