Php 比较从数据库获取的值和代码Yii
我试图比较我从用户那里得到的号码和数据库中存储的号码 基本上,我在做一个奖励系统,达到一定积分的用户将获得一个徽章。现在,我正在做的是,代码将计算用户拥有的点数,并将其发送到一个函数,该函数具有if-else来比较每个徽章的点数和值 我现在想做的是,通过将每个徽章的值放在db中使其更简单,以便下次我可以只更新db而不更新代码。下面是我现在正在做的事情Php 比较从数据库获取的值和代码Yii,php,sql,yii,Php,Sql,Yii,我试图比较我从用户那里得到的号码和数据库中存储的号码 基本上,我在做一个奖励系统,达到一定积分的用户将获得一个徽章。现在,我正在做的是,代码将计算用户拥有的点数,并将其发送到一个函数,该函数具有if-else来比较每个徽章的点数和值 我现在想做的是,通过将每个徽章的值放在db中使其更简单,以便下次我可以只更新db而不更新代码。下面是我现在正在做的事情 //check total points that the user has public function totalPointsValue($
//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');
}
因此,假设用户有600分,它将检查用户有权获得哪个徽章。如果$points>requirePoints,它将授予用户一个徽章。假设您有表名和型号徽章 编辑: 如果您只想要最接近用户获得的点数的徽章级别
function getBadgesOfUser($points){
$achive_badges = "";
$badges = CHtml::listData(Badge::model()->findAll(array(
'order'=>'requiredPoints', // ASC
'condition'=>'requiredPoints>=:rqp',
'params'=>array('rqp'=>$points))), 'badgeName', 'requiredPoints');
if(count($badges)>0){
//php 5.4
$achive_badges = array_keys($badges)[0]; // I don't use array_shift in this line because it would cause the warning raising in some cases.
//following for PHP 5.3
$temp = array_keys($badges); $achive_badges = $temp[0];
}
return $achive_badges;
}
我在查询中使用比较运算符进行排序,这意味着
requirePoints
列的数据类型必须是数字(int、decimal等)。我没有尝试过这段代码,但我有一个问题,最小值和最大值是什么。不太明白@TelvinEach badge level需要用户获得该徽章可能获得的赚取积分表标准(最小-最大值)。例如:徽章“超过500”需要最小=500&最大=1000,最大=1000,因为您的表格记录表明1000是徽章500i的下一级点;'我以前没看过你的作品。假设点是750,我不想要2个值的数组。我只想要500多岁的徽章,而不是100多岁的徽章。谢谢@Telvin。我会试试看。从外观上看,它应该可以工作,除非你把模型名改为城市,呵呵。将进行更新。代码不起作用。我根据我的数据库更改了它,但仍然没有;不行。但它也没有显示任何错误。想帮忙吗?
function getBadgesOfUser($points){
$achive_badges = "";
$badges = CHtml::listData(Badge::model()->findAll(array(
'order'=>'requiredPoints', // ASC
'condition'=>'requiredPoints>=:rqp',
'params'=>array('rqp'=>$points))), 'badgeName', 'requiredPoints');
if(count($badges)>0){
//php 5.4
$achive_badges = array_keys($badges)[0]; // I don't use array_shift in this line because it would cause the warning raising in some cases.
//following for PHP 5.3
$temp = array_keys($badges); $achive_badges = $temp[0];
}
return $achive_badges;
}