在2个数字之间查找%


Finding a % between 2 numbers

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

所以我有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的列,我想做的是获得他们离下一级别所需的exp的百分比,所以这就是我目前所拥有的。。。

<?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%

然后我想把%放进一个引导程序进度条中。

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

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

非常简单:只需要减去$startXP,如下所示:

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

然后您可以直接回显$percent-值。:)

你可以这样做:

$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>

您可以尝试以下操作:

<?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数据。