Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9069279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:23:16+00:00 2026-06-16T17:23:16+00:00

I developed this function to help me quickly grab information from the database about

  • 0

I developed this function to help me quickly grab information from the database about my user(s). My question is what would be the best wasy to have it return only a single row when I only need one user or the result when I need more than one.

/**
 * 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();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T17:23:17+00:00Added an answer on June 16, 2026 at 5:23 pm

    Better split your function into several functions doing small easy to correct understand queries.
    Create a function with one argument for example:

    /**
     * Return user that match given ids
     * @param array $uids the list of users to find
     * @return array - the users that matched the list
     * @throws DatabaseException on any error
     */
    public function listUserById( array $uids )
    {
    
    }
    
    /**
     * Return user that match given id and status
     * @param array $uids the list of users to find
     * @param array $status the list of user status to also find
     * @return array - the users that matched the list
     * @throws DatabaseException on any error
     */
    public function listUserByIdAndStatus( array $uids, array $status )
    {
      ///code
    }
    

    Or you can update documentation to indicate that your function return “lists” ( always arrays ).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question has developed off an answer here . My question therefore is what
I would really appreciate your help in this. I have been trying to get
I need to compile an input string (from a database) as a function and
This is a urgent question to be answered quickly. I'm developing iPhone application to
I -with some stackoverflow users help- have developed this tooltip script using Jquery and
I developed a plugin that appear people information and in the end of this
I developed one asp.net application then copy this application and put on another machine
I use this tab view that developed base on jQuery: https://d2o0t5hpnwv4c1.cloudfront.net/001_Tabbed/site/jQuery.html# I change the
I have developed a Phone Gap application using this link . Application is running
In this case, I've developed a CSS code for this web application ..and sometimes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.