Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 05-10-2005, 05:33 AM
wirewolf's Avatar
wirewolf wirewolf is offline
 
Join Date: Jun 2004
Location: New York City
Posts: 74
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default sql query coding question to select and/or update userfield

Need some help with writing a query to check the condition of two userfields in vbulletin after a user has uploaded photos in photopost. This query will be in a photopost php file (the file used just after a user has finished uploading). The photopost tables and the vbulletin tables are in the same database (makes it a little easier).

The scenario:
I have two userfields in vbulletin that if true (value = 1). field#x will display a link to that users photo gallery in the postbit and the member profile. field#xx will dispay the last photo uploaded as a thumbnail in the users' postbit with a link to that users' photo. Both of these are now user sectable in the user options (UserCP), and in the Admin user menu, but I would like this to be set automatically when the user uploads.

If this is the first time a user has uploaded photos, then the two values of these fields will be ' ' (null). But if the user has previously uploaded then most likely these values will be '1' (true). Most likely already set by me or the user.

So the first step is to check for the values of both fields. Then, based on their conditions, set or don't set the new value

The varible for the uploading user in the photopost script is $upuserid.
$userid is the variable (PRIMARY KEY) used by vbulletin in the userfield table.

This is what I have so far, but I'm stuck on the the two 'if' conditionals. And should I use UPDATE or INSERT INTO
Also, in the pohotopost scripting, they use - ppmysql_query - instead of mysql_query. Should I use the same syntax to query the vbulletin table.

PHP Code:
$query "SELECT fieldx, fieldxx FROM vbulletin_prefix_userfield WHERE userid = $upuserid";
$get_fields mysql_query($query);
list( 
$fieldx$fieldxx ) = mysql_fetch_row($get_fields);
mysql_free_result($get_fields);
if (
$fieldx == 1) {

} else {

$query "UPDATE vbulletin_prefix_userfield SET fieldx=1  WHERE userid = $upuserid";

}

if (
$fieldxx == 1) {

} else {

$query "UPDATE vbulletin_prefixuserfield SET fieldxx=1  WHERE userid = $upuserid";


This is the my first time writing this type of code from scratch, so any assistance would be greatly appreciated.
John
Reply With Quote
  #2  
Old 05-10-2005, 08:04 AM
Marco van Herwaarden Marco van Herwaarden is offline
 
Join Date: Jul 2004
Posts: 25,415
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

You can replace the whole code block above with 1 line:
PHP Code:
$query "UPDATE vbulletin_prefixuserfield SET fieldx=1, fieldxx=1  WHERE userid = $upuserid"
When you go through this code, you always want to set fieldx and fieldxx to the value of 1, regardless of the previous value. So there is no need to retrieve old values first. Ifi i understand you correct that is.
Reply With Quote
  #3  
Old 05-10-2005, 11:25 AM
wirewolf's Avatar
wirewolf wirewolf is offline
 
Join Date: Jun 2004
Location: New York City
Posts: 74
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Hi MarcoH64,
So, regardless if the value of both fields is already - '1' -, this will just overwrite?
PHP Code:
$query "UPDATE vbulletin_prefixuserfield SET fieldx=1, fieldxx=1  WHERE userid = $upuserid"
John
Reply With Quote
  #4  
Old 05-10-2005, 11:39 AM
Marco van Herwaarden Marco van Herwaarden is offline
 
Join Date: Jul 2004
Posts: 25,415
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Yes, this just tells MySQL to change these fields to the value you supply, regardless of their previous value.
Reply With Quote
  #5  
Old 05-10-2005, 01:15 PM
wirewolf's Avatar
wirewolf wirewolf is offline
 
Join Date: Jun 2004
Location: New York City
Posts: 74
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Odd, didn't work. I used my id as a test. I first went to my UserCp and unchecked (set to null) those two field options. Went into the database to view the change in the userfield table. My two fields, fieldx and fieldxx where indeed null. I then went and uploaded a test image in photopost. Everything went fine. No sql or php errors generated, so I assume that the syntax of the UPDATE query is correct. However, I went back to the database, but the two field values were still null, no value (1) was set. So, it seems that query was not executed.

Here's the block of code at the end of the photopost upload script, with my UPDATE query included:
PHP Code:
$query "UPDATE vbulletin_prefix_userfield SET field9=1, field18=1  WHERE userid = $upuserid";

    if (
$Globals['ppostcount'] == "yes")
        
inc_user_posts();

if (
$Globals['usenotify'] == "yes" && $User['adminedit'] = ) {
       
$User['userid'] = $upuserid;
        }

    if (
$Globals['usenotify'] == "yes" && $User['userid'] > ) {
        if (
$notify == "yes") {
            
$query "SELECT id FROM {$Globals['pp_db_prefix']}photos WHERE userid={$User['userid']} AND bigimage='$realname'";
            
$resulta ppmysql_query($query,$link);
            list( 
$photoid ) = mysql_fetch_row($resulta);
            
ppmysql_free_result($resulta);

            
$query "INSERT INTO {$Globals['pp_db_prefix']}notify (id,userid,photo) values(NULL,$upuserid,$photoid)";
            
$resulta ppmysql_query($query,$link);

        }
    }
    
    return( 
$lastphotoid );
}

?> 
If the value is already 'null', does the query need some kind of other statement prior to setting the value of '1' ?

BTW, Thank you for the time you are taking to assist me, John
Reply With Quote
  #6  
Old 05-10-2005, 02:22 PM
Marco van Herwaarden Marco van Herwaarden is offline
 
Join Date: Jul 2004
Posts: 25,415
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

And where did you execute $query?
Reply With Quote
  #7  
Old 05-10-2005, 03:06 PM
wirewolf's Avatar
wirewolf wirewolf is offline
 
Join Date: Jun 2004
Location: New York City
Posts: 74
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by MarcoH64
And where did you execute $query?
Not quite sure what you mean. The $query is near the end of the photopost upload script as shown above. Both photopost and vbulletin use the same database, but with different prefixes.

The user has just uploaded photo(s). The next steps:
If this is the first time he has uploaded photo(s), the existing values are null, then set the two userfields (x and xx) for this user to a value of 1 (a vbulletin table)

If he has uploaded photo(s) before then do not set the two userfields, as they are already set to the value of 1, or maybe just overwrite.

The remaining steps in this script are (these lines of code were already in the original photopost script):
increase user post count in the vbulletin user postcount? -yes, no (a vbulletin table)

notify section of code (subscription). If the user wants an email if someone comments on one of their photos (photopost tables)

End of script
Reply With Quote
  #8  
Old 05-10-2005, 03:07 PM
Marco van Herwaarden Marco van Herwaarden is offline
 
Join Date: Jul 2004
Posts: 25,415
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Try addingthe following after the first line:
PHP Code:
$resulta ppmysql_query($query,$link); 
Reply With Quote
  #9  
Old 05-10-2005, 04:27 PM
wirewolf's Avatar
wirewolf wirewolf is offline
 
Join Date: Jun 2004
Location: New York City
Posts: 74
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

BINGO!!! :up: :classic:
Did the trick. Did a bunch of test uploads, using different conditions (upload as me, upload for another user, with the two values set as null, both set as 1, etc) Works like a charm!
Thank you very much for your help MarcoH64. I'm going to post this over at Photopost (with kudos to you). There are other vbulletin owners with photopost integration (or photopost owners with vbulletin integration depending on your point of view), that have similar conditions and would certainly be interested in having this block of code.
Again, Much thanks
John
Reply With Quote
  #10  
Old 05-10-2005, 07:26 PM
Marco van Herwaarden Marco van Herwaarden is offline
 
Join Date: Jul 2004
Posts: 25,415
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

We would prefer if you (also) posted your hack here on vbulletin.org and put a link to here on photopost. But htat is your own choice.
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:27 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.05293 seconds
  • Memory Usage 2,282KB
  • Queries Executed 13 (?)
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
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (5)bbcode_php
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (10)post_thanks_box
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (10)post_thanks_postbit_info
  • (10)postbit
  • (10)postbit_onlinestatus
  • (10)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_postinfo_query
  • fetch_postinfo
  • 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
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete