我正在开发一个自定义 joomla 组件,它只是将一个带有两个字段(id,points)的点表添加到数据库中...... id 是主键,我试图将它设置为与用户 joomla 用户 id 相同...
我需要一个函数来返回用户的点数和另一个将用户点更新为给定值的函数......我成功创建了第一个名为:
getUserPoints()
它返回给定用户的点数......然后我启动了第二个更新函数,称为:
updateUserPoints()
这是目前“不一致”的部分,我认为我错了......我只是想制作一个简单的函数来更新当前用户的积分值......而且我也在尝试处理如果没有记录的情况......然后创建一个!......我通过在 updateUserPoints() 函数中创建一个名为的函数来做到这一点:
newUserCheck()
它应该检查用户是否有积分,然后回复真假:
$db->setQuery("SELECT points FROM #__mytable WHERE id = '".$user->id."'");
$db->query();
$points = $db->loadResult();
if($points){
return false;
} else {
return true;
}
然后更新创建新记录或更新现有记录...此代码几乎可以工作!...它在三个测试中对我有用...但是当我添加第二个用户时,它似乎有点不一致.. .它会成功创建一个新用户并给他们5分....然后当我再次运行它进行更新时,它会说“更新积分”但它似乎并没有真正更新数据库中的积分值?!
有一个或没有现有用户,它似乎工作正常!无论如何......欢迎提出建议......我确信我在这里做错了,但我想让它正常工作!我是 MVC 编程的新手,你可能会说:P
这是整个 joomla 控制器:
<?php defined('_JEXEC') or die('');
// userprofilepoints CONTROLLER
jimport('joomla.application.component.controller');
jimport('joomla.user.helper');
class userprofilepointsController extends JController {
function getUserPoints(){
$values = JRequest::get("post");
$userIdNumber = $values['userIdNumber'];
$user = & JFactory::getUser();
$db = JFactory::getDBO();
$db->setQuery("SELECT points FROM #__mytable WHERE id = '".$user->id."'");
$db->query();
$points = $db->loadResult();
echo $points;
}
function updateUserPoints(){
function newUserCheck(){
$values = JRequest::get("post");
$userIdNumber = $values['userIdNumber'];
$user = & JFactory::getUser();
$db = JFactory::getDBO();
$db->setQuery("SELECT points FROM #__mytable WHERE id = '".$user->id."'");
$db->query();
$points = $db->loadResult();
if($points){
return false;
} else {
return true;
}
}
if(newUserCheck() == false){
$values = JRequest::get("post");
$userIdNumber = $values['userIdNumber'];
$newPointsAmount = $values['newPointsAmount'];
$user = & JFactory::getUser();
$db = JFactory::getDBO();
$db->setQuery("UPDATE #__mytable SET points = '".$newPointsAmount."' WHERE id = '".$user->id."'");
$db->query();
//echo getUserPoints();
//$points = $db->loadResult();
echo "Points Updated";
} else if(newUserCheck() == true) {
$values = JRequest::get("post");
$userIdNumber = $values['userIdNumber'];
$user = & JFactory::getUser();
$db = JFactory::getDBO();
$db->setQuery("INSERT INTO #__mytable SET id = $user->id, points = 5");
$db->query();
echo "New User Created!";
}
}
}
最后是我的 html 和 javascript 视图!......谢谢!:)
<?php defined('_JEXEC') or die();
// MAIN PAGE TEMPLATE
jimport('joomla.user.helper');
$user = & JFactory::getUser();
$profile = JUserHelper::getProfile($user->id);
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('#updateBTN').click(function(){
updateUserPoints();
});
function getUserPoints(){
//Post the following to the controller
$.post('http://www.mysite.com/index.php?format=raw&option=com_userprofilepoints&task=getUserPoints', {
userIdNumber: <?php echo $user->id; ?>
}, function(data) {
alert(data);
});
}
function updateUserPoints(){
//Post the following to the controller
$.post('http://www.mysite.com/index.php?format=raw&option=com_userprofilepoints&task=updateUserPoints', {
userIdNumber: <?php echo $user->id; ?>,
newPointsAmount: 555444 //random hardcoded points amount to test with
}, function(data) {
alert(data);
});
}
});
</script>
<div id="main_block">
<p>Points Page:</p>
<br />
<button id="updateBTN">Trigger Update</button>
</div>