找到2个数字之间的%

时间:2016-05-07 12:35:35

标签: php twitter-bootstrap

所以我有一个用户具有健身水平的游戏,我想知道我是否可以使用健身系统做下面这样的事情..

所以我有20个健身水平,全部列在下面。

100 XP
200 XP
500 XP
1,000 XP
2,000 XP
3,200 XP
4,500 XP
6,500 XP
9,000 XP
12,000 XP
15,500 XP
20,000 XP
25,000 XP
32,000 XP
40,000 XP
50,000 XP
52,000 XP
70,000 XP
100,000 XP
200,000 XP

所有这些信息都存储在一个数据库中,我还有一个名为current_fitness_xp的用户列,我想要做的就是得到它们与下一级的needed_xp的距离的百分比,所以这个就是我到目前为止......

<?php
// Test script (Lets pretend we're level 5...)
$startXP = 2000; // Would be the current levels needed_xp
$currentXP = 2623; // Would be the current amount of xp..
$endXP = 3200; // Would be the next levels needed_xp

// it would output something near 50%

然后我想把%放入bootstrap进度条。

4 个答案:

答案 0 :(得分:2)

玩家进入下一关卡的可以使用以下公式计算:

$progress = ($currentXP - $startXP) / ($endXP - $startXP) * 100;

答案 1 :(得分:1)

这很简单:你只需要减去$startXP,就像这里:

<?php
$cXP = ($currentXP - $startXP);
$eXP = ($endXP - $startXP);
$percent = (100 / $eXP * $cXP);
?>

然后你可以回显$percent - 值。 :)

答案 2 :(得分:1)

你可以这样做:

$percent = round(($currentXP - $startXP) / ($endXP - $startXP) * 100);
// $percent is 51.91666. Round-function makes it 52.

<div class="progress-bar progress-bar-info" role="progressbar" aria-valuenow="<?php echo($percent); ?>"
  aria-valuemin="0" aria-valuemax="100" style="width:50%">
    <?php echo($percent); ?>
  </div>
</div>

所以基本上它看起来像这样:

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="progress">
  <div class="progress-bar progress-bar-info" role="progressbar" aria-valuenow="52"
  aria-valuemin="0" aria-valuemax="100" style="width:50%">
    52%
  </div>
</div>

答案 3 :(得分:0)

您可以尝试这样的事情:

<?php
$currentXP = 2623;
$endXP = 3200;

//Calculate your percentage:
$percentage = ($currentXP / $endXP) * 100;
?>

<div class="progress">
  <div class="progress-bar" role="progressbar" aria-valuenow="<?php echo $percentage; ?>" aria-valuemin="0" aria-valuemax="100" style="width: <?php echo $percentage; ?>%;">
    <?php echo $percentage; ?>%
  </div>
</div>

显然,您需要使用MySQLi或PDO等为您的用户从数据库中获取XP数据。

相关问题