0

ユーザーから取得した番号とデータベースに保存されている番号を比較しようとしています。

基本的に、一定のポイントに達したユーザーにバッジが与えられる報酬システムを行っています。今、私がやっていることは、コードはユーザーが持っているポイントを計算し、if else を持つ関数に送信して、ポイントと各バッジの値を比較することです。

私が今やりたいことは、各バッジの値をデータベース内に置くことで簡単にすることです。これにより、次回はコードを更新せずにデータベースを更新することができます。以下は私が今していることです。

//check total points that the user has
public function totalPointsValue($userId) {

    $value = Yii::app()->db->createCommand()
        ->select('sum(totalPoints) as pointsSum')
        ->from('fndn_UserTotal')
        ->where('userId =:id', array(':id'=>$userId))
        ->queryRow();

    $totalPoints = $value['pointsSum'];

    return $totalPoints;
}

//checks whether the user points is enough for a badge
public function checkEligable($userId){

    $points = $this->totalPointsValue($userId);

    //is not eligable for a badge
    if ($points <100){
        error_log(" $userId has less than 100 points. Number of points is $points ");
    }

    //eligable for the over 100 points badge
    elseif ($points > 100 && $points <1000){
        error_log(" $userId is eligable for over-100 badge. Number of points is $points ");


    }

    //eligable for the over 1000 points badge
    elseif ($points > 1000 && $points <2000){
        error_log(" $userId is eligable for over-1000 badge. Number of points is $points ");


    }

    //eligable for the over 2000 points badge
    else {
        error_log(" $userId is eligable for over-2000 badge. Number of points is $points ");


    }

    error_log(print_r($points, true), 3, 'debug.log');

}

コードがデータベース内の値をチェックし、$points と比較するようにしたいと考えています。以下は私のバッジデータベースです。

==============

id         badgeName        requiredPoints
1          over100            100
2          over500            500
3          over1000           1000 
4          over2000           2000 

したがって、ユーザーが 600 ポイントを持っているとしましょう。ユーザーが受け取る権利があるバッジを確認します。$points > requirePoints の場合、ユーザーにバッジが付与されます。

4

1 に答える 1