gpt4 book ai didi

php - 在 RSform 中按用户组过滤

转载 作者:搜寻专家 更新时间:2023-10-31 22:02:29 24 4
gpt4 key购买 nike

我在 Joomla 中使用 RSform 创建一个表单来输入数据。该表单有一个用于选择用户的下拉菜单,该字段使用表单中的以下代码获取数据。

我想做的是让列表只显示特定用户组中的用户名。实际用户存储在表 #__users 中,具有称为 id 的唯一 ID,组存储在表 #__usergroups 中,唯一 ID也称为 id。我希望能够过滤列表以仅显示用户组 2 中的那些用户。

有人可以帮忙吗?

提前致谢

// Prepare the empty array
$items = array();
// Prepare the database connection
$db = JFactory::getDbo();
// Keep this if you'd like a "Please select" option, otherwise comment or remove it
$items[] = "|Please Select[c]";

// Run the SQL query and store it in $results
$db->setQuery("SELECT id, name, email FROM #__users");
$results = $db->loadObjectList();

// Now, we need to convert the results into a readable RSForm! Pro format.
// The Items field will accept values in this format:
// value-to-be-stored|value-to-be-shown
// Eg. m|M-sized T-shirt
foreach ($results as $result) {
$value = $result->name;
$label = $result->name;
$items[] = $value.'|'.$label;
}

// Multiple values are separated by new lines, so we need to do this now
$items = implode("\n", $items);

// Now we need to return the value to the field
return $items;

最佳答案

您需要将用户映射到 usergroup_map 表,如下所示:

$groupID = 2;
$db = JFactory::getDbo();

$query = $db->getQuery(true);
$query->select('a.*')
->from($db->quoteName('#__users') . ' AS a')
->join('LEFT', $db->quoteName('#__user_usergroup_map') . 'AS map2 ON map2.user_id = a.id')
->group($db->quoteName(array('a.id', 'a.username')))
->where('map2.group_id = ' . (int) $groupID);
$db->setQuery($query);
$results = $db->loadObjectList();

foreach($results as $result) {
echo $results->username;
}

这将只显示属于当前设置为 2$groupID 的用户的所有用户名,您可以相应地更改它。

所以你的最终和完整代码将是这样的:

$items = array();
$groupID = 2;
$db = JFactory::getDbo();

$query = $db->getQuery(true);
$query->select('a.*')
->from($db->quoteName('#__users') . ' AS a')
->join('LEFT', $db->quoteName('#__user_usergroup_map') . 'AS map2 ON map2.user_id = a.id')
->group($db->quoteName(array('a.id', 'a.username')))
->where('map2.group_id = ' . (int) $groupID);
$db->setQuery($query);
$results = $db->loadObjectList();

$items[] = "|Please Select[c]";

foreach ($results as $result) {
$value = $result->username;
$label = $result->username;
$items[] = $value.'|'.$label;
}

$items = implode("\n", $items);

return $items;

关于php - 在 RSform 中按用户组过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25994468/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com