Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.
Not a member yet? Click here to register.
Forgot Password?

function or class to lookup userdata of other users

Asked Modified Viewed 1,313 times
E
Elvenelf
E
  • Member, joined since
  • Contributed 184 posts on the community forums.
  • Started 19 threads in the forums
  • Started this discussions
asked
Member

Is there something that provides userdata array for other users already in place? I haven't found it if it is, so I made me a class to help me out fetch this information.

When modding the system to do my bidding for this project I'm working on, I need usernames, user level, last login date, stuff like that, from given user based on ID because I am showing a list.... and I am doing various stages and I have to write this stuff repeatedly, so when I saw I'd have to repeatedly query the db to get this data I knew it was time to make something up....

Find the attached file, its a class you can put in the /includes/classes folder

I named it UserDataHelper, but y'all can rename it to whatever you like.

I also added the methods I needed at the moment, but you can, of course, expand it to fetch whatever user information you need. For instance, cross-table data, like comments count, download file uploaded count, forum post count, etc.

You can also collaborate in making it better by going onto the github gist here or posting your improved version on this thread.

P.S. As a shortcut, when instantiating the class, you must provide a user id there, so it pre-fetches user data onto an array var in the class, then you can call getter methods that lookup array keys from that array. To see what kind of data is pulled, look into the fetchUserData method, it has the query. I am purposefully avoiding to SELECT * as a security measure, because I don't want to pull hashes or algo identification of any kind, so I am naming the columns I want explicitly.

Usage would be like:
$user_object = new UserDataHelper($id);
$username = $user_object->getUserName();


Note to self (lol): The getter methods could be made static so it is easier to call them up once the class has been instantiated.
Edited by Elvenelf on 24-09-2014 16:30,
Elvenelf attached the following file:
userdatahelper.zip [No information available / 170 Downloads]
0 replies

2 posts

J
JoiNNN
J
JoiNNN 10
  • Veteran Member, joined since
  • Contributed 850 posts on the community forums.
  • Started 100 threads in the forums
answered
Veteran Member

Nice to see you're coming up with such useful code, keep posting your ideas and maybe they will make it in the core before .03 release, would definetly love that (hear that Domi? :) Give it a thought)
Now a few things about the class. I agree that unsafe data such as the password should be left out but adding each column to the query is not the best solution, infusions and userfields add their own data and getting that using your class won't be a an option.
What you could do:
- add all the unsafe columns to an array
- do request all the data (including the unsafe)
- remove the data flagged as unsafe from the array
- continue...

I m not that much into classes (it is the right approach in this case tho, don't get me wrong) but will I be able to do this
$data = $user_object->getUserData();
$some_row = $data['some_row'];
in one line without you having to create functions for every situation?

Also you might wanna use isnum instead of is_numeric since the last one allows other characters in numbers and might be unsafe. And there is the reason why isnum doesn't allow decimals, whenever it was used the numbers were supposed to be JUST numbers and is safer.
0 replies
E
Elvenelf
E
  • Member, joined since
  • Contributed 184 posts on the community forums.
  • Started 19 threads in the forums
  • Started this discussions
answered
Member

I understand your take on user fields, and you are right. This is not the best approach for the public usage of this type of class, not for general CMS use... but in my case, I modded the user lvl field to be decimal, because I have various types of administrators in my project. This led me to modify a LOT of maincore functions that work with user level and permissions. So I end up using is_numeric because my user levels have one decimal place.

For the sake of my project, I have one other type of regular member at 101.1, and two other admin types at 102.2 and 102.3....

I have to admit I posted the class more in the spirit of igniting interest into making it better for the cms. As it is, it only benefits my needs at the time I wrote it... I also added a new method after the post... to return usernames within a link to their profile page.
public function getUserNameWithProfileLink(){
   return "<a href='".BASEDIR."profile.php?lookup=".$this->requested_user_data['user_id']."'>".$this->getUserName()."</a>";
}


You are correct about the is_numeric function allowing other characters. It takes other representations of numbers that php will ultimately evaluate as numeric values. Some of those are, "x", "e" and "b"... depending on their location in the string of course it would return true or false even if for our need it has a letter in there when we don't want it.

Regarding your usage sample.... You could for instance, do something like that. I have the array as a variable you can get from the class obj. All it needs is a proper id when instantiating the class.

So... for user id 42 you would be doing something like...
$user_obj = new UserDataHelper(42);
$userdata_ar = $user_obj->getUserData();
$username = $userdata_ar['user_name'];


I am aware that, as it is now, if you added it, it would fire an sql error because my user columns include a couple of custom user fields I created (user_tel, user_company)... but the idea remains there for toying with it.... I may fix this up and make it more decent for using with a vanilla cms install... but that would have to wait some until I'm done coding for the project...

It's just that I am used to work like this... if I'm using this cms, I share back code that I make that I find useful myself, as other devs of infusions or modders might find it useful too. Giving something back to the project and stuff. ;)
0 replies

Labels

None yet

Statistics

  • Views 0 views
  • Posts 2 posts
  • Votes 0 votes
  • Topic users 2 members

2 participants

E
E
  • Member, joined since
  • Contributed 184 posts on the community forums.
  • Started 19 threads in the forums
  • Started this discussions
J
J
JoiNNN 10
  • Veteran Member, joined since
  • Contributed 850 posts on the community forums.
  • Started 100 threads in the forums

Notifications

Track thread

You are not receiving notifications from this thread.

Related Questions

Not yet