如何使用php在joomla数据库中设置限制?用户应按第2组和第6组显示

qv7cva1a  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(305)

如何用php在joomla数据库中设置限制?如果登录的用户属于组2,我应该只看到3个值;如果登录的用户属于组6,我应该只看到n个数据中的9个值。

<?php 
    $dbo = JFactory::getDBO(); 
    $user=& JFactory::getuser(); 

    $groups = $user->get('groups');

    foreach($groups as $id) {

     echo "<p>Group ID is:" . $id . "</p>";

      // how to assign the limit ?

    if($id ==2){    
        echo  "<ul id='jchat_conference_userslist'></ul>"; //  3 users
    } elseif($id ==6){  
        echo "<ul id='jchat_conference_userslist'></ul>";   // 9 users  
    }

    }?>

这是查询

<?php 

$query = $this->_db->getQuery(true)
            ->select('*')
            ->from($this->_db->quoteName('#__users'))
            ->where($this->_db->quoteName('id') . ' = ' . (int) $userId);
                $query->setLimit(1); // limit is not working
        $this->_db->setQuery($query);
        $data = (array) $this->_db->loadAssoc();

?>
iqjalb3h

iqjalb3h1#

由于您已使用query编辑了问题,下面的代码应该可以解决您的问题:

$query = $this->_db->getQuery(true);
$query->select('*');
$query->from($this->_db->quoteName('#__users'));
//$query->where($this->_db->quoteName('id') . ' = ' . (int) $userId);
// Note : Above where condition is commented purposely as there will be always only one user with given userid 
$groups = $user->get('groups');
//Note : When user will have the 2 & 3 groups then it will always add the setLimit 2.
if(in_array("2",$groups))
{
    $query->setLimit('2');
}
else if(in_array("3",$groups))
{
    $query->setLimit('3'); // Set limit of 3 on query
}
$this->_db->setQuery($query);
$data = (array) $this->_db->loadAssoc();

还有一些问题,
为什么要根据用户组添加多个记录的where条件。因为在joomla的用户表id将是pk,因此它不会有多个具有相同用户id的用户,因此这里不使用setlimit
你想达到什么目的?

相关问题