2016-05-07 15 views
0

,所以我有一個遊戲,一個用戶擁有一個健身水平,我想知道如果我可以做這樣的事情下面的健身系統之間的%..發現2號

所以我有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% 

然後,我想要將該%放入引導程序進度欄。

+0

所以才呼應它作爲你的用戶酒吧的價值? –

+0

什麼?我正在嘗試獲取%,但是如何獲得%?我不會總是知道數字,這只是一個例子,不是我的實際水平或XP .. – TheGod39

+0

您需要計算百分比,然後將其作爲百分比值的值回顯出來。 –

回答

2

%的球員的進步到一個新的水平可以用下面的公式計算:

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

這很簡單:你只需要減去$startXP,喜歡這裏:

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

然後你可以只回聲$percent - 值。 :)

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>

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> 

顯然,你需要抓住從數據庫中的數據XP使用的MySQLi或PDO等