vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 General Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=111)
-   -   Cron not quite working (https://vborg.vbsupport.ru/showthread.php?t=108653)

Nullifi3d 02-22-2006 08:09 PM

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.

Marco van Herwaarden 02-22-2006 08:15 PM

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.

Nullifi3d 02-22-2006 09:16 PM

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'");



Marco van Herwaarden 02-22-2006 10:35 PM

Well with $armyinfo on the second line it looks already much better to me.

Nullifi3d 02-23-2006 12:35 AM

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.

Marco van Herwaarden 02-23-2006 08:26 AM

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.

Nullifi3d 02-23-2006 09:10 AM

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.

Marco van Herwaarden 02-23-2006 09:17 AM

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.

Nullifi3d 02-23-2006 09:56 AM

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?


All times are GMT. The time now is 01:05 AM.

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.01131 seconds
  • Memory Usage 1,789KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (10)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (9)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.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/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.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
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete