【问题标题】:Get percentage per level获取每个级别的百分比
【发布时间】:2017-04-06 21:52:32
【问题描述】:

我创建了一个基于帖子的级别系统。

Level 1 = 1-25 posts
Level 2 = 26-50 posts
Level 3 = 51-250 posts, etc...

我还想显示一个进度条

通常你会这样:

$author_posts = 15;
$progress = ($author_posts * 100) / 25; //(level 1)

那么进度百分比就是60%。

但是如果用户已经到达level 3,我应该使用什么?

if( $author_posts >= '250' ) {
    $progress   = '100';
} elseif( $author_posts < '51' ) {
    $progress   = '0';
} else {
    $progress   = // what should I use here?
}


<div class="progress-bar" style="width:<?php echo esc_attr( $progress ); ?>%;"></div>

【问题讨论】:

  • 你能解释一下你所说的百分比是什么意思吗?到下一个级别是百分比吗?
  • 基础数学 = ($author_posts / 250) *100
  • 当前关卡(level3)内的百分比。或者在第 3 级被清除之前完成了多少百分比。我认为应该是这样的,但我不确定:$progress ( $author_posts * 100 ) / ( 250 - 51 );

标签: php progress-bar percentage


【解决方案1】:

您包含的 if 块意味着用户在达到该级别的下限之前处于 0% 的进度。那么我们是否可以假设,一旦该边界被破坏,该边界之下的任何先前帖子都不会计入百分比?这意味着只有 51 到 250 个帖子才算作百分点,范围为 200 个帖子(含)。所以 1 个帖子 = 0.5%。

如果是这样

$progress = round( ( ( $author_posts - 51 ) / 200 ) * 100 )

51 个帖子 = 0%

52 个帖子 = 1%(四舍五入)

200 个帖子 = 75%

这个公式的可重复使用版本可能看起来像

$progress = round( ( ( $author_posts - $lower ) / ( ( $upper - $lower ) + 1 ) ) * 100 )

$upper 和 $lower 边界在每个级别内重新定义。

【讨论】:

  • 是的,完全正确。在您的公式中,添加了一个额外的 ),导致错误。 :)
  • 对不起,我没有检查就编辑了。更新。如果这回答了您的问题,请接受。
  • 已接受。谢谢!
【解决方案2】:

使用这个:

if( $author_posts >= '250' ) {
    $progress   = '100';
} elseif( $author_posts < '51' ) {
    $progress   = '0';
} else {
    $progress   = (($author_posts - 50) / 200) * 100;
}

<div class="progress-bar" style="width:<?php echo esc_attr( $progress ); ?>%;"></div>

【讨论】:

    猜你喜欢
    • 2011-05-13
    • 2020-10-19
    • 2018-07-12
    • 1970-01-01
    • 1970-01-01
    • 2018-12-24
    • 1970-01-01
    • 2021-11-28
    • 2019-04-09
    相关资源
    最近更新 更多