Go Back   vb.org Archive > vBulletin Modifications > Archive > vB.org Archives > General > Member Archives
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools
Details »»

Version: , by JJR512 JJR512 is offline
Developer Last Online: Jun 2006 Show Printable Version Email this Page

Version: Unknown Rating:
Released: 02-24-2002 Last Update: Never Installs: 0
 
No support by the author.

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:
  $usersettings $DB_site->query_first("SELECT * FROM weather_usersettings WHERE userid=$bbuserinfo[userid]");
  if (
$usersettings[userid]="") {
    
// do stuff
  
} else {
    
// do other stuff
  

It works. But for some reason I've got this weird feeling that there's a better way. Can someone please let me know what that better way is, or else put my mind at ease that I'm doing it the right way now?

Show Your Support

  • This modification may not be copied, reproduced or published elsewhere without author's permission.

Comments
  #2  
Old 02-24-2002, 04:49 PM
Admin's Avatar
Admin Admin is offline
Coder
 
Join Date: Oct 2023
Location: Server
Posts: 1
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Code:
if ($usersettings=$DB_site->query_first("SELECT * FROM weather_usersettings WHERE userid=$bbuserinfo[userid]")) {
	// user is there
} else {
	// user is not there
}

/* ### OR ### */

$usersettings=$DB_site->query_first("SELECT * FROM weather_usersettings WHERE userid=$bbuserinfo[userid]")
if ($DB_site->num_rows()>0) {
	// user is there
} else {
	// user is not there
}
Very simple.
Reply With Quote
  #3  
Old 02-24-2002, 04:52 PM
Admin's Avatar
Admin Admin is offline
Coder
 
Join Date: Oct 2023
Location: Server
Posts: 1
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

You can also add an exclamation mark before $usersettings in the first method, to check if the user is not there.
Reply With Quote
  #4  
Old 02-24-2002, 05:09 PM
JJR512's Avatar
JJR512 JJR512 is offline
 
Join Date: Oct 2001
Location: Glen Burnie, MD, USA
Posts: 710
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Question: Using your second method, in this line:
PHP Code:
if ($DB_site->num_rows()>0) { 
Does the query variable, $usersettings, not need to go in between the ()'s of the function num_rows()?

I mean, should it look like this:
PHP Code:
if ($DB_site->num_rows($usersettings)>0) { 
Or is it correct exactly as you have it? Just making sure.
Reply With Quote
  #5  
Old 02-24-2002, 05:11 PM
JJR512's Avatar
JJR512 JJR512 is offline
 
Join Date: Oct 2001
Location: Glen Burnie, MD, USA
Posts: 710
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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?
Reply With Quote
  #6  
Old 02-24-2002, 05:22 PM
JJR512's Avatar
JJR512 JJR512 is offline
 
Join Date: Oct 2001
Location: Glen Burnie, MD, USA
Posts: 710
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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:
  $usersettings $DB_site->query_first("SELECT * FROM weather_usersettings WHERE userid=$bbuserinfo[userid]");
  if (!isset(
$usersettings[userid])) {
    
// user NOT in table
  
} else {
    
// user IS in table
  

Reply With Quote
  #7  
Old 02-24-2002, 05:46 PM
Admin's Avatar
Admin Admin is offline
Coder
 
Join Date: Oct 2023
Location: Server
Posts: 1
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
  #8  
Old 02-24-2002, 06:35 PM
JJR512's Avatar
JJR512 JJR512 is offline
 
Join Date: Oct 2001
Location: Glen Burnie, MD, USA
Posts: 710
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
  #9  
Old 02-25-2002, 04:53 AM
Admin's Avatar
Admin Admin is offline
Coder
 
Join Date: Oct 2023
Location: Server
Posts: 1
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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]")) {
and:
Code:
if ([high]$usersettings=[/high]$DB_site->query_first("SELECT * FROM weather_usersettings WHERE userid=$bbuserinfo[userid]")) {
The first one only checks if the query returned a row, but the second one also stores what's returned in the variable.

I'm using this all the time, works great.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 07:54 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
  • Page Generation 0.06901 seconds
  • Memory Usage 2,290KB
  • Queries Executed 22 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (3)bbcode_code
  • (4)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)modsystem_post
  • (1)navbar
  • (6)navbar_link
  • (120)option
  • (9)post_thanks_box
  • (9)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (9)post_thanks_postbit_info
  • (8)postbit
  • (9)postbit_onlinestatus
  • (9)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete