2017-04-06 102 views
0

我已经创建了基于职位的级别系统。获取每个级别的百分比

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> 
+0

你能澄清你的百分比是什么意思?是百分比直到下一个级别? –

+0

basic math =($ author_posts/250)* 100 – nogad

+0

它是当前级别(level3)内的百分比。或者等到3级被清除后,完成多少比例。我认为它应该是这样的,但我不知道:'$ progress($ author_posts * 100)/(250 - 51);' – kiarashi

回答

2

您包含的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边界在每个级别内重新定义。

+0

是的,这是完全正确的。在你的公式中添加了一个额外的''',导致了一个错误。 :) – kiarashi

+0

对不起,我没有检查编辑。更新。请接受,如果这回答你的问题。 – domwrap

+0

已接受。谢谢! – kiarashi

0

使用此:

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>