Php 每级获得百分比

Php 每级获得百分比,php,progress-bar,percentage,Php,Progress Bar,Percentage,我创建了一个基于帖子的等级系统 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% 但是,如果用户已经达到级别3,我应该使用什么 if( $author_posts >= '250'

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

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%

但是,如果用户已经达到
级别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>
if($author\u posts>='250'){
$progress='100';
}elseif($author_posts<'51'){
$progress='0';
}否则{
$progress=//我应该在这里使用什么?
}

包含的
if
块意味着用户在达到该级别的下限之前的进度为0%。那么,我们是否可以假设,一旦该边界被打破,以前在该边界下的帖子都不算作百分比?这意味着只有51到250个职位被计算为百分比,范围为200个职位(含)。因此,1个岗位=0.5%

如果是的话

51个职位=0%

52个职位=1%(四舍五入)

200个职位=75%

此公式的可重用版本可能如下所示

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

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

包含的
if
块意味着用户在到达该级别的下边界之前的进度为0%。那么,我们是否可以假设,一旦该边界被打破,以前在该边界下的帖子都不算作百分比?这意味着只有51到250个职位被计算为百分比,范围为200个职位(含)。因此,1个岗位=0.5%

如果是的话

51个职位=0%

52个职位=1%(四舍五入)

200个职位=75%

此公式的可重用版本可能如下所示

$progress = round( ( ( $author_posts - $lower ) / ( ( $upper - $lower ) + 1 ) ) * 100 )
其中,
$upper
$lower
边界在每个级别内重新定义。

使用以下方法:

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>
if($author\u posts>='250'){
$progress='100';
}elseif($author_posts<'51'){
$progress='0';
}否则{
$progress=($author_posts-50)/200)*100;
}
使用以下命令:

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>
if($author\u posts>='250'){
$progress='100';
}elseif($author_posts<'51'){
$progress='0';
}否则{
$progress=($author_posts-50)/200)*100;
}

你能澄清你所说的百分比是什么意思吗?在下一个级别之前是百分比吗?基础数学=($author_posts/250)*100在当前级别(级别3)内是百分比。或者,在第3级被清除之前,完成了多少百分比。我认为应该是这样,但我不确定:
$progress($author_posts*100)/(250-51)你能澄清你所说的百分比是什么意思吗?在下一个级别之前是百分比吗?基础数学=($author_posts/250)*100在当前级别(级别3)内是百分比。或者,在第3级被清除之前,完成了多少百分比。我认为应该是这样,但我不确定:
$progress($author_posts*100)/(250-51)是的,完全正确。在您的公式中添加了一个额外的
,导致了错误:对不起,我没有检查就编辑了。更新。如果这回答了您的问题,请接受。接受。非常感谢。是的,完全正确。在您的公式中添加了一个额外的
,导致了错误:对不起,我没有检查就编辑了。更新。如果这回答了您的问题,请接受。接受。非常感谢。