所以我知道这很简单,但是我一直在撞墙,试图解决这个问题。我想在每个循环的底部显示一个标尺,除了最后一个。如果我有确切的记录数量,我可以让它工作,但如果我有更少的记录则不能。例如,如果要显示的最大数字是10但是只有5条记录,我希望第4条记录后的分隔符。同样地,如果有20个结果但是max是10,那么我希望它在第9个之后。
<?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; ?
答案 0 :(得分:2)
// <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++;
}
这需要$max-1
或count($subscriberIDs)-1
中的较小者,根据定义,它将是您将迭代的 last 项目。如果您有超过$max
项,那么这将是$max-1
,如果您的项目少于$max
,那么这将是count(.)-1
。
然后,在迭代期间,只要当前项目不 last 项目,if
语句就会打印<hr>
。< / p>