XP和找平系统PHP MYSQL [英] Xp and leveling system PHP MYSQL

查看:77
本文介绍了XP和找平系统PHP MYSQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经查看了所有提出的问题以及答案,但是我似乎找不到最适合我的答案.我试图做的是使系统在用户达到某个xp限制时进入下一个级别.并显示直到下一个XP为止需要多少XP.

Ive looked at all the question that have been asked and with the answer, but i cannot seem to find a answer that suits me the best. What im trying to do is work on making a system that when a user is at a certain xp limit that go to the next level. and it shows how much xp is needed till the next xp.

所以

lvl1 = 0 => lvl2 = 256 => lvl3 = 785 => lvl4 = 1656 => lvl5 = 2654

我将如何去做,因此,如果xp达到一定数量,则显示该数量以及下一级别需要多少xp.

how would i go about doing that, so if xp is at certain amount show the amount and how much xp is needed for the next level.

推荐答案

您可以尝试执行以下操作:

You can try something like this:

我们的数据库中有users_xp表,其中包含user_xp_id(主键-自动递增),user_iduser_xp_amount(默认值:0)字段.当我们要更新用户xp金额时,我们应该这样做:

We have users_xp table in our db, with user_xp_id (primary key - auto increment), user_id and user_xp_amount (default value: 0) fields. When we want to update the user xp amount we should do it like:

$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');

function update_user_xp($user_id, $amount, $mysqli) {
    $mysqli->query("UPDATE users_xp 
                    SET user_xp_amount=user_xp_amount+" . $amount . " 
                    WHERE user_id='$user_id'");
}

// we call this function like:
update_user_xp(4, 10, $mysqli); // user_id: 4, update with 10 points

当我们想要获取实际用户xp数量时,可以从数据库表中获取它

when we want to get the actual user xp amount we can get it from our db table

function get_user_xp($user_id, $mysqli) {
    $sql = $mysqli->query("SELECT user_xp_amount 
                           FROM users_xp 
                           WHERE user_id='$user_id'");
   $row = $sql->fetch_assoc();
   return $row['user_xp_amount'];

}

$xp = array('lvl1' => 0, 'lvl2' => 256, 'lvl3' => 785, 'lvl4' => 1656, 'lvl5' => 2654);

$my_xp = get_user_xp(4, $mysqli); // where 4 is my user id

for($i = 0; $i < count($xp); $i++) {
   if($my_xp == $xp[$i]) {
       echo 'I\'m on level ', ($i+1);
       break;
   }
   else {
       if(isset($xp[$i+1])) {
           if($my_xp > $xp[$i] && $my_xp <= $xp[$i + 1]) {
               echo 'My next level is ', ($i+2), ' and I need ', $xp[$i+1], ' more points for achieving it!';
               break;
            } else {
               echo 'My next level is ', ($i+1), ' and I need ', $xp[$i], ' more points for achieving it!';
               break;
            }
        }
    }
}

以后的修改:

CREATE TABLE `my_db_name`.`users_xp` (
`user_xp_id` BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`user_id` BIGINT NOT NULL ,
`user_xp_amount` BIGINT NOT NULL DEFAULT '0'
) ENGINE = InnoDB;

这篇关于XP和找平系统PHP MYSQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆