Welcome Guest, Not a member yet? Register   Sign In
Myth:Auth
#41

(This post was last modified: 11-15-2020, 01:54 PM by InsiteFX. Edit Reason: updated with error checking on user id )

I put this together tonight may help you.

PHP Code:
    // -------------------------------------------------------------------

    /**
     * getUser ()
     * -------------------------------------------------------------------
     *
     * Usage:
     * NOTE: $id can be left blank it will then user the auth_helper
     *       user_id() method. But this only works if user is logged in.
     *
     * $user = $this->getUser($id);
     * $user = $this->getUser();
     *
     * echo $user->username;
     *
     * @param  int $id
     * @return object
     */
    
public function getUser(int $id 0) : object
    
{
        
$data = [];

        
// connect to the database.
        
$db db_connect();
        
$builder $db->table('users');

        
/**
         * check user id if it does not exsist we assign the admin id
         * to avoid error.
         */
        
if ($id == 0)
        {
            
// get the logged in user
            
if (user_id() != null)
            {
                
$id user_id();
            }
            else
            {
                
$id 1;
            }
        }

        
// get the users record.
        
$query $builder->where('id'$id)
        
                 ->get();

        
$results $query->getResult();

        foreach (
$results as $row)
        {
            
$data $row;
        }

        
$query->freeResult();

        return 
$data;
    } 

Returns the complete user object.

10-15-2020 - Update to do better error checking on the user id.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply


Messages In This Thread
Myth:Auth - by kilishan - 12-11-2019, 10:49 PM
RE: Myth:Auth - by MGatner - 12-12-2019, 04:31 AM
RE: Myth:Auth - by InsiteFX - 12-12-2019, 05:46 AM
RE: Myth:Auth - by Basic App - 12-17-2019, 07:33 AM
RE: Myth:Auth - by MGatner - 12-17-2019, 07:36 AM
RE: Myth:Auth - by Basic App - 12-17-2019, 08:52 AM
RE: Myth:Auth - by kilishan - 12-17-2019, 08:56 AM
RE: Myth:Auth - by skechav - 01-02-2020, 02:32 PM
RE: Myth:Auth - by BilltheCat - 02-02-2020, 06:18 PM
RE: Myth:Auth - by InsiteFX - 02-03-2020, 08:56 AM
RE: Myth:Auth - by BilltheCat - 02-03-2020, 09:45 AM
RE: Myth:Auth - by muuucho - 04-13-2020, 10:52 PM
RE: Myth:Auth - by destez - 04-21-2020, 01:30 AM
RE: Myth:Auth - by InsiteFX - 04-21-2020, 03:29 AM
RE: Myth:Auth - by destez - 04-21-2020, 03:36 AM
RE: Myth:Auth - by InsiteFX - 04-24-2020, 08:06 AM
RE: Myth:Auth - by destez - 04-24-2020, 03:29 PM
RE: Myth:Auth - by InsiteFX - 04-25-2020, 08:21 AM
RE: Myth:Auth - by destez - 04-25-2020, 08:41 AM
RE: Myth:Auth - by destez - 04-25-2020, 03:08 PM
RE: Myth:Auth - by nfaiz - 04-25-2020, 06:17 PM
RE: Myth:Auth - by dodjiD - 06-20-2020, 07:29 PM
RE: Myth:Auth - by jim1001 - 08-07-2020, 08:08 AM
RE: Myth:Auth - by InsiteFX - 08-08-2020, 10:36 AM
RE: Myth:Auth - by jim1001 - 08-09-2020, 10:53 AM
RE: Myth:Auth - by InsiteFX - 08-09-2020, 11:09 AM
RE: Myth:Auth - by jim1001 - 08-18-2020, 04:26 AM
RE: Myth:Auth - by jim1001 - 08-18-2020, 06:57 AM
RE: Myth:Auth - by InsiteFX - 08-18-2020, 08:53 AM
RE: Myth:Auth - by MGatner - 08-18-2020, 10:08 AM
RE: Myth:Auth - by jim1001 - 08-18-2020, 11:34 AM
RE: Myth:Auth - by jim1001 - 08-19-2020, 09:59 AM
RE: Myth:Auth - by InsiteFX - 08-19-2020, 12:08 PM
RE: Myth:Auth - by jim1001 - 08-19-2020, 01:23 PM
RE: Myth:Auth - by aschmitz - 11-08-2020, 09:33 AM
RE: Myth:Auth - by InsiteFX - 11-08-2020, 12:06 PM
RE: Myth:Auth - by aschmitz - 11-09-2020, 03:46 PM
RE: Myth:Auth - by MGatner - 11-10-2020, 05:43 AM
RE: Myth:Auth - by aschmitz - 11-10-2020, 08:42 AM
RE: Myth:Auth - by suvi - 11-12-2020, 05:27 AM
RE: Myth:Auth - by InsiteFX - 11-13-2020, 01:06 AM
RE: Myth:Auth - by suvi - 11-13-2020, 06:11 AM
RE: Myth:Auth - by InsiteFX - 11-13-2020, 01:08 PM
RE: Myth:Auth - by suvi - 11-15-2020, 09:19 AM
RE: Myth:Auth - by InsiteFX - 11-15-2020, 01:56 PM
RE: Myth:Auth - by suvi - 11-15-2020, 03:54 PM
RE: Myth:Auth - by lan89 - 12-05-2020, 01:39 PM
RE: Myth:Auth - by MGatner - 12-06-2020, 05:33 AM
RE: Myth:Auth - by lan89 - 12-06-2020, 06:26 AM
How to use the invitation feature? - by suvi - 12-30-2020, 06:29 AM
RE: Myth:Auth - by InsiteFX - 12-31-2020, 04:09 AM
RE: Myth:Auth - by suvi - 12-31-2020, 08:18 AM
RE: Myth:Auth - by MGatner - 12-31-2020, 08:20 AM
RE: Myth:Auth - by suvi - 12-31-2020, 12:41 PM
RE: Myth:Auth - by MGatner - 01-01-2021, 06:52 AM
RE: Myth:Auth - by suvi - 01-02-2021, 01:40 PM
RE: Myth:Auth - by klj - 01-08-2021, 03:42 AM



Theme © iAndrew 2016 - Forum software by © MyBB