php显示最大计数器数

时间:2017-04-10 16:38:20

标签: php counter

我在foreach(){} carrossel中有一个计数器,它显示当前的幻灯片。但是如何显示计数器的最大数量? 通过这种方式,我可以执行幻灯片1 of 3

 $count_slide = 0;

    foreach( $featured_posts as $post ) {
    $count_slide++;
   ?> <p> <?php echo $count_slide ?> - Show Max Value</p><?
    ...
    }

谢谢

2 个答案:

答案 0 :(得分:1)

您的旋转木马的片段是否在阵列中?

如果是这样,您可以计算($ array)以获得幻灯片的总数。

如果失败了,你可以在foreach的末尾增加一个计数器变量:

    $counterVar=0;
    foreach($this as $that) {
      // your code
    $counterVar++;
    }

答案 1 :(得分:1)

你可能正在寻找类似的东西:

<?php
$featured_posts = ['slide1', 'slide2', 'slide3', 'slide4', 'slide5', 'slide6', 'slide7'];

$counter = 0;
$maxCounter = count($featured_posts);
foreach ($featured_posts as $post ) {
    $counter = ($counter % $maxCounter) + 1;
    echo sprintf("slide %s of %s\n", $counter, $maxCounter);
}

上述代码的输出显然是:

slide 1 of 7
slide 2 of 7
slide 3 of 7
slide 4 of 7
slide 5 of 7
slide 6 of 7
slide 7 of 7