- This topic is empty.
-
AuthorPosts
-
May 4, 2014 at 7:08 pm #169205
chrisburton
ParticipantHey, guys. As some of you know I’m working on a comment system for the Kirby CMS. I’m having trouble wrapping my head around a few things. I’m trying to figure out the best way to check if a user exists (by their user ID) and if that user exists, check to see what needs to be updated. Whether it be a profile image or their name.
Can anyone spare a moment?
Also, I am trying to use the Kirby classes as much as possible. See here. I believe I need to use
db::select()
for this.May 4, 2014 at 7:29 pm #169206__
Participantbest way to check if a user exists (by their user ID)
I’d say something like
select 1 from user where id=:id
. Using the Kirby db class, I guess it would be something like:$id = "desired_user_id"; db::select( "name_of_user_table","'1'",["id"=>$id] ); // or, if Kirby can't deal with selecting literals db::select( "name_of_user_table","id",["id"=>$id] );
and …check to see what needs to be updated.
What do you mean by “needs to be updated”? not filled out yet? filled out too long ago?
Once you can qualify it, we should be able to build it into the query.
May 4, 2014 at 7:34 pm #169207chrisburton
Participant@traq I’m trying to compare what is in the session to what is in the database. If the UID exists in both, the session and the database, I know that user exists.
Variable for UID in session:
$user['uid']
.So the code should be the following?:
db::select( 'users,'1','uid'=>$user['uid'] );
After that check is complete, I guess I could just automatically update the name, link and image of the user even if their profile data is the same. I need to do this because when people change their profile images or usernames, those links are no longer valid. Checking them against each other may just be time wasted. Or should I do that?
Update: the following code echoes out to true:
// Check if user exists $exists = db::select('users','1',array('uid'=>$user['uid'])); if($exists) { echo "true"; } else { echo "error!"; }
May 4, 2014 at 7:46 pm #169208__
ParticipantI guess I could just automatically update the name, link and image of the user even if their profile data is the same.
If you’re already making a query, then the only extra overhead would be the size of the data you’re sending. A new username or profile image url isn’t much. MySQL is smart enough to ignore updates where the info doesn’t actually change.
The other option would be to store the old (original) data in the session as well as the user’s updated info. That way, you can compare the two and update the DB if necessary.
example using
array_diff_assoc
:$original = ["a"=>"A","b"=>"B","c"=>"Sea"]; $updated = ["a"=>"A","b"=>"B","c"=>"C"]; $needs_update = array_diff_assoc( $updated,$original ); // $needs_update contains: ["c"=>"C"]
edit:
the following code echoes out to true
Try id with an id that you know does not exist and see if it returns
false
. I read the docs ondb::select
, and for return value it just says “mixed,” so I don’t know whattrue
means in this case. It could be our1
cast to bool, or it could simply mean that the query didn’t return an error.May 4, 2014 at 7:55 pm #169209chrisburton
Participant@traq If MySQL is smart enough to do that, then I would rather go with
db::update
rather than compare data. Less markup to deal with.I’m having trouble with this one as it isn’t making sense to me since I need to update more than one.
Edit:
Try id with an id that you know does not exist and see if it returns false[…]
It echoes out false if I try and ID of
95
(random number).May 4, 2014 at 8:06 pm #169210__
ParticipantIt echoes out false if I try and ID of 95 (random number).
I …guess… it works, then.
I’m having trouble with this one as it isn’t making sense to me since I need to update more than one.
One row, or one field?
Is something wrong with
db::update( $table, ["name"=>$name, "link"=>$link, "image"=>$image], ["id"=>$id] );
?
May 4, 2014 at 8:09 pm #169211chrisburton
Participant@traq Actually, I forgot the syntax but looked at some old code of mine and remembered how to do it which is close to what you have.
My code:
db::update('users', array($user['name']=>'name',$user['image']=>'image',$user['link']=>'link'), array('uid' => $user['uid']));
Edit: Oops do I have it backwards?
May 4, 2014 at 8:12 pm #169212__
ParticipantOops do I have it backwards?
looks right.
BTW, sorry if it’s confusing, I just realized I’ve been using the (php 5.4+) array literal syntax in my examples.
It’s so nice not having to writearray( ... );
anymore : )May 4, 2014 at 8:16 pm #169213chrisburton
Participant@traq It was in fact backwards. I just tested with my Twitter account and it does update correctly. Finally moving forward! I really appreciate your help on this one. Now just need to figure out how to join tables.
BTW, sorry if it’s confusing, I just realized I’ve been using the (php 5.4+) array literal syntax in my examples. It’s so nice not having to write array( … ); anymore : )
No problem at all.
May 4, 2014 at 8:22 pm #169214__
ParticipantOh; I was looking at the arg order, not the key=>value order. Sorry.
May 4, 2014 at 8:29 pm #169215chrisburton
ParticipantThat was my fault. Thanks again, man.
May 4, 2014 at 8:51 pm #169216__
Participantno prob!
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.