Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 General Discussions
  #1  
Old 02-22-2006, 08:09 PM
Nullifi3d Nullifi3d is offline
 
Join Date: Apr 2004
Location: FL, USA
Posts: 215
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Cron not quite working

PHP Code:
$armysys $vbulletin->db->query_read("SELECT strike_action, defense_action, spy_rating, sentry_rating, attack_soldiers, defense_soldiers, untrained_soldiers, spies, sentries FROM " TABLE_PREFIX "armysys");
while (
$armysys $vbulletin->db->fetch_array($armysys)) {
    
$rank = ( ($armyinfo['strike_action'] + $armyinfo['defense_action'] + $armyinfo['spy_rating'] + $armyinfo['sentry_rating']) / (4) );
    
$gold = ( ($armyinfo['attack_soldiers'] * 15) + ($armyinfo['defense_soldiers'] * 15) + ($armyinfo['untrained_soldiers'] * 5) + ($armyinfo['spies'] * 10) + ($armyinfo['sentries'] * 10) );
    
$vbulletin->db->query_write("UPDATE " TABLE_PREFIX "armysys SET rank = '$rank', gold = gold + '$gold'");

I have tried so many variations of code that my brain hurts. I need this code to update the field rank in armysys with $rank (the average of the 4 fields you see in the variable). As well it neess to update the field gold with $gold (the sum of the fields in the variable). Depending on what I change it sometimes doesn't do anything, but it usually just updates ever row witht he same info.
Reply With Quote
  #2  
Old 02-22-2006, 08:15 PM
Marco van Herwaarden Marco van Herwaarden is offline
 
Join Date: Jul 2004
Posts: 25,415
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

PHP Code:
$armysys $vbulletin->db->query_read("SELECT strike_action, defense_action, spy_rating, sentry_rating, attack_soldiers, defense_soldiers, untrained_soldiers, spies, sentries FROM " TABLE_PREFIX "armysys"); 
Here you assign a SQL-Resource to $armysys.
PHP Code:
while ($armysys $vbulletin->db->fetch_array($armysys)) { 
And here you overwrite the same variable with the data of the first row retrieved.
Reply With Quote
  #3  
Old 02-22-2006, 09:16 PM
Nullifi3d Nullifi3d is offline
 
Join Date: Apr 2004
Location: FL, USA
Posts: 215
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

PHP Code:
$armysys $vbulletin->db->query_read("SELECT strike_action, defense_action, spy_rating, sentry_rating, attack_soldiers, defense_soldiers, untrained_soldiers, spies, sentries FROM " TABLE_PREFIX "armysys"); 
If I use Query_first it doesn't work.

Sorry, it's suppose to be:
PHP Code:
while ($armyinfo $vbulletin->db->fetch_array($armysys)) { 
I use this code elsewhere and it works fine:
PHP Code:
$links $db->query_read("SELECT pl_href AS href, pl_title AS title, pl_anchor AS anchor FROM " TABLE_PREFIX "user WHERE pl_href != ''");
while (
$link $db->fetch_array($links)) eval('$premium_links .= "' fetch_template('directory_premium_links') . '";'); 
This works fine to print out each row that $links stores. Why doesn't this query (whether or not I use query_read or query_first):
PHP Code:
 $armysys $vbulletin->db->query_read("SELECT strike_action, defense_action, spy_rating, sentry_rating, attack_soldiers, defense_soldiers, untrained_soldiers, spies, sentries FROM " TABLE_PREFIX "armysys");
while (
$armyinfo $vbulletin->db->fetch_array($armysys)) {
    
$rank = ( ($armyinfo['strike_action'] + $armyinfo['defense_action'] + $armyinfo['spy_rating'] + $armyinfo['sentry_rating']) / (4) );
    
$gold = ( ($armyinfo['attack_soldiers'] * 15) + ($armyinfo['defense_soldiers'] * 15) + ($armyinfo['untrained_soldiers'] * 5) + ($armyinfo['spies'] * 10) + ($armyinfo['sentries'] * 10) );
    
$vbulletin->db->query_write("UPDATE " TABLE_PREFIX "armysys SET rank = '$rank', gold = gold + '$gold'");

Reply With Quote
  #4  
Old 02-22-2006, 10:35 PM
Marco van Herwaarden Marco van Herwaarden is offline
 
Join Date: Jul 2004
Posts: 25,415
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Well with $armyinfo on the second line it looks already much better to me.
Reply With Quote
  #5  
Old 02-23-2006, 12:35 AM
Nullifi3d Nullifi3d is offline
 
Join Date: Apr 2004
Location: FL, USA
Posts: 215
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

well
PHP Code:
$armysys $vbulletin->db->query_first("SELECT strike_action, defense_action, spy_rating, sentry_rating, attack_soldiers, defense_soldiers, untrained_soldiers, spies, sentries FROM " TABLE_PREFIX "armysys");
while (
$armyinfo $vbulletin->db->fetch_array($armysys)) {
    
$rank = ( ($armyinfo['strike_action'] + $armyinfo['defense_action'] + $armyinfo['spy_rating'] + $armyinfo['sentry_rating']) / (4) );
    
$gold = ( ($armyinfo['attack_soldiers'] * 15) + ($armyinfo['defense_soldiers'] * 15) + ($armyinfo['untrained_soldiers'] * 5) + ($armyinfo['spies'] * 10) + ($armyinfo['sentries'] * 10) );
    
$vbulletin->db->query_write("UPDATE " TABLE_PREFIX "armysys SET rank = '$rank', gold = gold + '$gold'");

this does nothing
and
PHP Code:
$armysys $vbulletin->db->query_read("SELECT strike_action, defense_action, spy_rating, sentry_rating, attack_soldiers, defense_soldiers, untrained_soldiers, spies, sentries FROM " TABLE_PREFIX "armysys");
while (
$armyinfo $vbulletin->db->fetch_array($armysys)) {
    
$rank = ( ($armyinfo['strike_action'] + $armyinfo['defense_action'] + $armyinfo['spy_rating'] + $armyinfo['sentry_rating']) / (4) );
    
$gold = ( ($armyinfo['attack_soldiers'] * 15) + ($armyinfo['defense_soldiers'] * 15) + ($armyinfo['untrained_soldiers'] * 5) + ($armyinfo['spies'] * 10) + ($armyinfo['sentries'] * 10) );
    
$vbulletin->db->query_write("UPDATE " TABLE_PREFIX "armysys SET rank = '$rank', gold = gold + '$gold'");

this sets all ranks in all rows to 0 and totals up the gold for all rows and adds it to every row. At this time there are only 2 rows in the table.
Reply With Quote
  #6  
Old 02-23-2006, 08:26 AM
Marco van Herwaarden Marco van Herwaarden is offline
 
Join Date: Jul 2004
Posts: 25,415
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Well right now you are looping all rows in 'armysys'. Then for each row in that table, you are setting the values for all rows.

You should add a WHERE-clause to your UPDATE-statement, uniquely identifying the row you're working on.
Reply With Quote
  #7  
Old 02-23-2006, 09:10 AM
Nullifi3d Nullifi3d is offline
 
Join Date: Apr 2004
Location: FL, USA
Posts: 215
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I need to update every row in the table indepedantly, not just some of them. This is why i don't have a where clause. Every hour the cron is suppose to give every member that is signed up (row in armysys) gold and also suppose to update their rank.
Reply With Quote
  #8  
Old 02-23-2006, 09:17 AM
Marco van Herwaarden Marco van Herwaarden is offline
 
Join Date: Jul 2004
Posts: 25,415
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

What is happening with your current script is:
1. A row (member) is read from armysys
2. With the information of this row, you do some calculations
3. ALL ROWS (members) are updated with the outcome of teh calculation performed for 1 member.
4. goto -> 1.

You will need to use the memberid of the current row (for which you've done calculations) in the update WHERE-clause.

The way you have it now, all members will get the outcome of the calculation of the last member when this script is finished.
Reply With Quote
  #9  
Old 02-23-2006, 09:56 AM
Nullifi3d Nullifi3d is offline
 
Join Date: Apr 2004
Location: FL, USA
Posts: 215
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Sorry for my ignorance. I finally got it to work with:
PHP Code:
$armysys $vbulletin->db->query_read("SELECT userid, strike_action, defense_action, spy_rating, sentry_rating, attack_soldiers, defense_soldiers, untrained_soldiers, spies, sentries FROM " TABLE_PREFIX "armysys");
while (
$armyinfo $vbulletin->db->fetch_array($armysys)) {
    
$rank = ( ($armyinfo['strike_action'] + $armyinfo['defense_action'] + $armyinfo['spy_rating'] + $armyinfo['sentry_rating']) / (4) );
    
$gold = ( ($armyinfo['attack_soldiers'] * 15) + ($armyinfo['defense_soldiers'] * 15) + ($armyinfo['untrained_soldiers'] * 5) + ($armyinfo['spies'] * 10) + ($armyinfo['sentries'] * 10) );
    
$vbulletin->db->query_write("UPDATE " TABLE_PREFIX "armysys SET rank = '$rank', gold = gold + '$gold' WHERE userid = " $armyinfo['userid']);

I do have one other question though. This cron runs once every hour (through vbulletin) and I need to display only the minutes this specific cron has left before it runs again. How would I do this?
Reply With Quote
Reply

Thread Tools
Display Modes

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 04:34 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.04720 seconds
  • Memory Usage 2,288KB
  • 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
  • (10)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)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
  • (9)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