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

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

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

  1. <?php
  2. $dbo = JFactory::getDBO();
  3. $user=& JFactory::getuser();
  4. $groups = $user->get('groups');
  5. foreach($groups as $id) {
  6. echo "<p>Group ID is:" . $id . "</p>";
  7. // how to assign the limit ?
  8. if($id ==2){
  9. echo "<ul id='jchat_conference_userslist'></ul>"; // 3 users
  10. } elseif($id ==6){
  11. echo "<ul id='jchat_conference_userslist'></ul>"; // 9 users
  12. }
  13. }?>

这是查询

  1. <?php
  2. $query = $this->_db->getQuery(true)
  3. ->select('*')
  4. ->from($this->_db->quoteName('#__users'))
  5. ->where($this->_db->quoteName('id') . ' = ' . (int) $userId);
  6. $query->setLimit(1); // limit is not working
  7. $this->_db->setQuery($query);
  8. $data = (array) $this->_db->loadAssoc();
  9. ?>
iqjalb3h

iqjalb3h1#

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

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

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

展开查看全部

相关问题