【问题标题】:php show max number of counterphp 显示最大计数器数
【发布时间】:2017-04-10 16:38:20
【问题描述】:

我在 foreach(){} carrossel 中有一个计数器,它显示当前幻灯片。但是如何显示计数器的最大数量? 这样我就可以执行 slide 1 of 3 之类的操作。

 $count_slide = 0;

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

谢谢

【问题讨论】:

  • 我们应该怎么知道?您没有显示您的代码!

标签: php counter


【解决方案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

【讨论】:

  • 哦,有一个功能。我应该提到我正在使用 wordpress。非常感谢你。 :)
  • 即使是 wordpress 也不能把 php 毁得那么远,以至于你不能使用这样的功能 ;-)
  • 谢谢,我已经在使用它了:"$maxCounter = count($featured_posts);"
  • 你说得对。屏幕前的时间太长了。 ...感觉很愚蠢....再次感谢您
【解决方案2】:

您的轮播的幻灯片是否在数组中?

如果有,可以通过count($array)得到幻灯片的总数。

如果做不到这一点,您可以在 foreach 结束时增加一个计数器变量:

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

【讨论】:

  • 是的,但我想每次我回到第一张幻灯片向我展示最大值,例如:幻灯片 1-3 幻灯片 2-3 幻灯片 3-3 那样我做不到它。谢谢:)
猜你喜欢
  • 2015-06-25
  • 2011-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多