获取每个级别的百分比
Get percentage per level
我创建了一个基于帖子的等级系统。
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>
您包含的 if
块意味着用户在达到该级别的下限之前处于 0% 进度。那么我们是否可以假设一旦突破该边界,之前 post 中的 none 将计为百分比?这意味着只有 posts 51 到 250 算作百分比,范围为 200 posts(含)。所以 1 post = 0.5%.
如果是
$progress = round( ( ( $author_posts - 51 ) / 200 ) * 100 )
51 posts = 0%
52 posts = 1% (rounded up)
200 posts = 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>
我创建了一个基于帖子的等级系统。
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>
您包含的 if
块意味着用户在达到该级别的下限之前处于 0% 进度。那么我们是否可以假设一旦突破该边界,之前 post 中的 none 将计为百分比?这意味着只有 posts 51 到 250 算作百分比,范围为 200 posts(含)。所以 1 post = 0.5%.
如果是
$progress = round( ( ( $author_posts - 51 ) / 200 ) * 100 )
51 posts = 0%
52 posts = 1% (rounded up)
200 posts = 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>