• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Userlib - User Library

#21
[eluser]Jamongkad[/eluser]
[quote author="Code Arachn!d" date="1185311958"]Love this library - it's very flexible and has a lot of room for growth!

On tweak I added was some more flexibility in getData - I set it up so that username or select criteria could be left off allowing for a return of all fields of a particular row and/or certain fields from many rows this way I can list one or more users quickly.

Code:
function getData( $username = '', $what = '*' )
{
  $where_clause = (!empty($username)) ? $where_clause = ' WHERE username=\'' . mysql_real_escape_string( $username ) . '\'' : '';
  $lcheck = $this->CI->db->query( 'SELECT ' . mysql_real_escape_string( $what ) . ' FROM `users` ' . $where_clause);
  if( $lcheck->num_rows( ) > 0 ) {
   return $lcheck->result_array( );
  } else {
   return 'Username or row does not exist.';
  }
}
[/quote]

Awesome would it be too much trouble to show me how to use this function in my view?(Sorry PHP newb here) a code sample would be nice. Like lets say once the user logged in the 'view' will greet him/her by his/her first name?

#22
[eluser]Code Arachn!d[/eluser]
Sure, so if you setup your call like this:
Code:
$user_list = $this->userauth->getData();
what you end up with an 3d array of users and their information
Code:
Array (
[0] => Array (
  [id] => 1
  [username] => user1
  [password] => 55c3b5386c486feb662a0785f340938f518d547f
  [name] => name 1
  [email] => [email protected]
  [ip] => 255.255.255.255
  [status] => 3
  )
[1] => Array (
  [id] => 2
  [username] => user2
  [password] => 55c3b5386c486feb662a0785f340938f518d547f
  [name] => name 2
  [email] => [email protected]
  [ip] => 255.255.255.255
  [status] => 3
  )
)
This then gives you the ability to foreach out your array as such
Code:
foreach($user_list as $user) {
  echo $user['name'] . ' - '.$user_perm_level[$user['status']].'<br />';
}

*** one to keep in mind is that I setup the user perm level as a config item so you'll basically want to do something like this before your foreach...
Code:
$user_perm_level = $this->config->item('user_perm_level');

#23
[eluser]Code Arachn!d[/eluser]
One thing I forgot - the changes I added are for if you're trying to list multiple users... in the case of pulling one user back - do the same thing just make sure you set your $username - and then it'll loop though just one record since you've limited the return...

On a side note - I personally get a little more vague on my where clause setups in my models and build the "semi" sql in the code call that way i can customize my where statement any way I need on the fly - it's probably not the best practice but it works for me.

#24
[eluser]Jamongkad[/eluser]
[quote author="Code Arachn!d" date="1185313775"]One thing I forgot - the changes I added are for if you're trying to list multiple users... in the case of pulling one user back - do the same thing just make sure you set your $username - and then it'll loop though just one record since you've limited the return...
[/quote]

Check I modified the source code of Userlib with your new getData function and so far it's working. When you say set the $username I think this is what you're talking about?

Code:
$user_list = $this->userlib->getData($username);

foreach( $user_list as $user )
{
echo $user['username'];
}

#25
[eluser]iniweb[/eluser]
big big thx's Smile But how logout? session_destroy?

#26
[eluser]Doosje[/eluser]
[quote author="iniweb" date="1187104632"]big big thx's Smile But how logout? session_destroy?[/quote]
just put:
Code:
function logout()
    {
        $this->CI->session->sess_destroy();
    }
at the end of the file
(before:
Code:
}
// END User Class
?&gt;
)
and you're done.

you can call it like
Code:
$this->userlib->logout();

#27
[eluser]boteeka[/eluser]
There is an inconsistency in your class: namely, the table names are hardcoded. This is very disturbing when I set up my CI install to use table prefixes, and I expect it to be used in every library, helper, etc.

#28
[eluser]Tom Schlick[/eluser]
Code:
function getData( $user, $what )
    {
        if(is_numeric($user))
        {
            $lcheck = $this->CI->db->query( "SELECT " . mysql_real_escape_string( $what ) . " FROM `user` WHERE
                                            ID='" . mysql_real_escape_string( $user ) . "'" );
            if( $lcheck->num_rows( ) == 1 )
            {
                $data = $lcheck->row( );
                return $data->$what;
            } else {
                return 'User or row does not exist.';
            }
        }
        else
        {
            $lcheck = $this->CI->db->query( "SELECT " . mysql_real_escape_string( $what ) . " FROM `users` WHERE
                                            username='" . mysql_real_escape_string( $user ) . "'" );
            if( $lcheck->num_rows( ) == 1 )
            {
                $data = $lcheck->row( );
                return $data->$what;
            } else {
                return 'Username or row does not exist.';
            }
        }
    }

i added the is_numeric to the get data so you will be able to put a user id number or just put in their username and both will work in the same function! Smile (the only problem is that if their username is only numbers it will cause an error. so just make it so that your users have to include a letter in thier name)

#29
[eluser]Unknown[/eluser]
Sorry for My bad English

how can user on my site view you own login (or email) at page?

<code>echo $this->userlib->getData( "_what write this_", "_what write this_");</code>

I am noob, help me please.
Thx.

#30
[eluser]Unknown[/eluser]
Awesome, man. Thanks, it looks great!


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.