![]() |
Suppose I have a table with a bunch of columns. The first is userid. A row gets added to the database when a user goes through and fills out a form. Prior to the user filling out the form, there is no row in the database with his/her userid.
When the form is displayed, I need to determine whether or not the person who requested the form to be displayed has a row in that table, so the code can determine whether to set the various fields to some default values (if there is no row for that user) or to the values the user already selected. So far, I've come up with this: PHP Code:
|
Code:
if ($usersettings=$DB_site->query_first("SELECT * FROM weather_usersettings WHERE userid=$bbuserinfo[userid]")) { |
You can also add an exclamation mark before $usersettings in the first method, to check if the user is not there.
|
Question: Using your second method, in this line:
PHP Code:
I mean, should it look like this: PHP Code:
|
Also, I would be making the first part be for if the user is NOT there, so instead of being >0, should it be =0 or ==0?
|
Well, I was having some trouble doing testing to see if I could answer my own questions. So I tried this, and this works too, and unless you tell me there's any particular reason why this isn't a good idea, I'll do this:
PHP Code:
|
It works, but I find my first method more elegant (less lines, I guess that's why). :)
No, you shouldn't pass $usersettings to num_rows() because it's not a result set, it's an associative array. If you call num_rows() without any parameters it will use the last executed query, which is what we want. And turn it into <1 if want to see if the user is not there. :) |
I guess the reason why I didn't use the first method was that I need the query executed separately from the if...else block. I also need the if...else block to be in the order of if the user isn't there, else if the user is there. The reason for this is that if the user is not there, variables for $usersettings ($usersettings[userid], usersettings[setting1], etc.) are explicitly defined with a set of default values in the "if user isn't there" part of the code. I actually changed it so it isn't an if...else, just an if. If the user isn't there, the default values are put into the variables, and either way, execution procedes to the next part, using either the default values or the values from the query.
|
But the query is executed! :)
There's a difference between this: Code:
if ($DB_site->query_first("SELECT * FROM weather_usersettings WHERE userid=$bbuserinfo[userid]")) { Code:
if ([high]$usersettings=[/high]$DB_site->query_first("SELECT * FROM weather_usersettings WHERE userid=$bbuserinfo[userid]")) { I'm using this all the time, works great. :) |
All times are GMT. The time now is 09:02 PM. |
Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information | |
---|---|
|
|
![]() |
|
Template Usage:
Phrase Groups Available:
|
Included Files:
Hooks Called:
|