我开发了这个功能来帮助我快速从数据库中获取有关我的用户的信息。我的问题是,当我只需要一个用户时,最好只返回一行,或者当我需要多个用户时返回结果。
/**
* get_users function.
*
* @access public
* @param array $params (default: array();)
* possible array keys:
* user_ids - either an array of user ids or a single user id
* user_status_ids - either an array of user status ids or a single user status id
* user_role_ids - either an array of user role ids or a single user role id
*
* @return object/NULL
* Should return user_id, username, CONCAT(users.first_name, users.last_name, email_address, lock_date, user_status_name, user_role_name
*/
public function get_users($params = array())
{
$this->db->select('users.user_id');
$this->db->select('users.username');
$this->db->select('CONCAT(users.first_name, " ", users.last_name) AS full_name', FALSE);
$this->db->select('users.password');
$this->db->select('users.password_hash');
$this->db->select('users.email_address');
$this->db->select('users.lock_date');
$this->db->select('users.user_status_id');
$this->db->select('users.user_role_id');
$this->db->select('user_statuses.user_status_name');
$this->db->select('user_roles.user_role_name');
$this->db->from('users');
$this->db->join('user_statuses', 'user_statuses.user_status_id = users.user_status_id');
$this->db->join('user_roles', 'user_roles.user_role_id = users.user_role_id');
//checking to see if any $params are attempting to be passed
if (count($params) > 0)
{
//start title specific selection
if (isset($params['user_ids']))
{
//if you only have one integer.
if (is_numeric($params['user_ids']))
{
$this->db->where('users.user_id', $params['user_ids']);
}
else
{
if (is_array($params['user_ids']))
{
$a = 0;
foreach($params['user_ids'] as $user_id)
{
if ($a == 0)
{
$this->db->where('users.user_id', $user_id);
}
else
{
$this->db->or_where('users.user_id', $user_id);
}
$a++;
}
}
}
}
//start title specific selection
if (isset($params['usernames']))
{
//if you only have one integer.
if (is_string($params['usernames']))
{
$this->db->where('users.username', $params['usernames']);
}
else
{
if (is_array($params['usernames']))
{
$a = 0;
foreach($params['usernames'] as $username)
{
if ($a == 0)
{
$this->db->where('users.usernames', $username);
}
else
{
$this->db->or_where('users.username', $username);
}
$a++;
}
}
}
}
//start title specific selection
if (isset($params['user_status_ids']))
{
//if you only have one integer.
if (is_numeric($params['user_status_ids']))
{
$this->db->where('users.user_status_id', $params['user_status_ids']);
}
else
{
if (is_array($params['user_status_ids']))
{
$a = 0;
foreach($params['user_status_ids'] as $user_status_id)
{
if ($a == 0)
{
$this->db->where('users.user_status_id', $user_status_id);
}
else
{
$this->db->or_where('users.user_status_id', $user_status_id);
}
$a++;
}
}
}
}
//start title specific selection
if (isset($params['user_role_ids']))
{
//if you only have one integer.
if (is_numeric($params['user_role_ids']))
{
$this->db->where('users.user_role_id', $params['user_role_ids']);
}
else
{
if (is_array($params['user_role_ids']))
{
$a = 0;
foreach($params['user_role_ids'] as $user_role_id)
{
if ($a == 0)
{
$this->db->where('users.user_role_id', $user_role_id);
}
else
{
$this->db->or_where('users.user_role_id', $user_role_id);
}
$a++;
}
}
}
}
}
$query = $this->db->get();
return $query->result();
}