我正在开发一个自定义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>
答案 0 :(得分:2)
从newUserCheck()
$db->setQuery("SELECT points FROM #__mytable WHERE id = '".$user->id."'");
$db->query();
$points = $db->loadResult();
实际应该是:
$db->setQuery("SELECT points FROM #__mytable WHERE id = '".$user->id."'");
$points = $db->loadResult();
loadResult()
将运行查询。