我设计了非常简单的关卡脚本,这里的代码是:
$LEVEL['0'] = 0; //0XP - basic value of main level.
$LEVEL['1'] = 500; //500XP
$LEVEL['2'] = 1100; //+600
$LEVEL['3'] = 1800; //+700
$LEVEL['4'] = 2500; //+800
$LEVEL['5'] = 3300; //+900
$LEVEL['6'] = 4200; //+1000
$LEVEL['7'] = 5200; //+1100
$LEVEL['8'] = 5300; //+1200
$LEVEL['9'] = 6500; //+1300
$LEVEL['10'] = 7800; //+1400 - increase scale rate by 100XP per each tenth level
$LEVEL['11'] = 9200; //+1600
$LEVEL['12'] = 10800; //+1800
$LEVEL['13'] = 12600; //+2000
$LEVEL['14'] = 14600; //+2200
$LEVEL['15'] = 16800; //+2400
$LEVEL['16'] = 19200; //+2600
$LEVEL['17'] = 21800; //+2800
$LEVEL['18'] = 24600; //+3000
$LEVEL['19'] = 27600; //+3200
$LEVEL['20'] = 30800; //+3500
$test = 18000;
for($i = 0; $i < count($LEVEL); $i++){
if($test > $LEVEL[$i++]){
$currentLevel = $i++;
}
}
echo $currentLevel-1;
使用$ test变量可以很好地工作但是当我从SQL数据库中选择示例值时,它会在第十次迭代时突然停止。字段具有以下设置:
experience INT(6) UNSIGNED DEFAULT 0
我回应了价值并且它应该如此印刷,例如,如果它的10800它打印10800所以选择了正确的值但它显示的不是11级而是9,想法?谢谢。< / p>
答案 0 :(得分:1)
$levels = array(
0,
500, //500XP
1100, //+600
1800, //+700
2500, //+800
// --- all the XPs
);
$test = 18000;
foreach ($levels as $level => $xp) {
if ($test < $xp) {
break;
}
}
echo $level;
数据库值没有什么特别之处..只需使用该值而不是$test
答案 1 :(得分:0)
我正在使用从数据库中挑选的值。这是产生这种奇怪行为的代码块
//Using loaded user ID, load level of user and skill points.
$queryLevel = mysqli_query($connection, "SELECT * FROM users_levels WHERE id = '$userId'");
$userLevel = mysqli_fetch_assoc($queryLevel);
$userExperience = $userLevel['experience'];
$userPoints = $userLevel['points'];
//Level is 0 if for loop will not find value higher than any given in LEVEL array.
$currentLevel = 0;
for($i = 0; $i < count($LEVEL); $i++){
if($userExperience > $LEVEL[$i++]){
$currentLevel = $i++;
}
}
这是我的数据库表的结构:
id INT(50) PRIMARY KEY
experience INT(6) UNSIGNED
points INT(2)
utf8_polish_ci
在该表中是一行,其中包含以下值:
+----------+----------+----------+
| id |experience| points |
+----------+----------+----------+
| 3 | 16200 | 0 |
+----------+----------+----------+