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

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 02-12-2009, 08:43 AM
Gandalf-LoJ Gandalf-LoJ is offline
 
Join Date: Feb 2005
Location: Sussex, UK
Posts: 15
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Array loop and datamanager save() problem

Hey all, I've stumbled across an issue that has got me totally stumped.

What I have is a script that will run via the scheduled task system that will update a users membergroups based on whether they have a particular award assigned to them. (and removes if they no longer have that award)

This was working great when it was done for a couple of awards as I simply hardcoded in the award and group id's.

Now, as there are many awards and groups to assign I modified the script and put all of the values into an array, which will then loop through that and update everyone accordingly. Sounds simple, and when I echo to screen all of the data it looks like it is working perfectly. However it is not updating everyone as per what is being displayed on the screen.

It is almost as if the
PHP Code:
$userdm->set('membergroupids'$membergroupids);
$userdm->save(); 
is not saving everyone. What is really confusing is that it will for some people, but not for others.

As mentioned above, if I hard code the group and award id's it works perfectly. So it appears to be something to do with the looping of the array that is upsetting the datamanager. Just wondering if this is a limitation or just something I'm doing wrong!

I've tried several variants of looping the array from foreach, do while, and finally a simple count array and then a for loop until count. All the same result!

Below is the first half of the script that has the add users. The delete is essentially the same problem. This is the latest test with the count array and for until count option.

Here's a sample of the many echos (being very thorough!) that is produced when the script is run. The first ID user 66 is being assigned to group 50 as he has that award, but when I check the account after the script has been run it is not there even though it says it's been saved.

Any ideas at all?

Code:
18|
Award=3, Group=50
Existing Groups=27,43 | UserID=66 Membergroups=27,43,50 | Account updated! | UserID=66 Membergroups=27,43,50
Existing Groups=52 | UserID=806 Membergroups=52,50 | Account updated! | UserID=806 Membergroups=50,52
Existing Groups=37,48,50 | UserID=234 Membergroups=37,48,50 | Account updated! | UserID=234 Membergroups=37,48,50
Existing Groups=48,50 | UserID=800 Membergroups=48,50 | Account updated! | UserID=800 Membergroups=48,50
PHP Code:
// ######################## SET PHP ENVIRONMENT ###########################
error_reporting(E_ALL & ~E_NOTICE);
if (!
is_object($vbulletin->db))
{
    exit;
}

// ########################### SET VARIABLES ##############################

// Awards - This controls the foreach statement and is used as the array index for $grouparr
$awardarr = array(35691213141718202124252931323839);

// Groups - Here we have the relevant award as the index and the group that award belongs to as the value
$grouparr = array(
                    
=> 50,
                    
=> 38,
                    
=> 50,
                    
=> 53,
                    
12 => 23,
                    
13 => 43,
                    
14 => 43,
                    
17 => 51,
                    
18 => 37,
                    
20 => 53,
                    
21 => 53,
                    
24 => 53,
                    
25 => 53,
                    
29 => 48,
                    
31 => 40,
                    
32 => 39,
                    
38 => 46,
                    
39 => 52
                
);

// ########################################################################
// ######################### USERGROUP UPDATES ############################
// #########################    GIVE ACCESS    ############################
// ########################################################################

foreach ($awardarr AS $key => $awardval)
{
    
$aw2gr $grouparr[$awardval];
    echo 
"<br><br>Award=".$awardval.", Group=".$aw2gr;
    
// check for award and update secondary usergroup if required
    
$giveawards $vbulletin->db->query_read("
        SELECT award_id, userid 
        FROM " 
TABLE_PREFIX "award_user
        WHERE award_id = 
$awardval
    "
);
    
    if (
$vbulletin->db->num_rows($giveawards) > 0)
    {
        while (
$giveawardsrow $vbulletin->db->fetch_array($giveawards))
        { 
// for each award, check to see if they are a member of the relevant group and update accordingly
            
$userdm =& datamanager_init('User'$vbulletinERRTYPE_ARRAY); 
            
$userinfo fetch_userinfo($giveawardsrow['userid']);
            
$userdm->set_existing($userinfo); 
            
$membergroupids $userdm->fetch_field('membergroupids');
            echo 
"<br>Existing Groups=".$membergroupids." | ";
            if (
$membergroupids != "")
            {
                
$withcomma "," $aw2gr;
                
$trailingcomma $aw2gr ",";
                
$membergroupids str_replace("$withcomma"""$membergroupids);
                
$membergroupids str_replace("$trailingcomma"""$membergroupids);
                
$membergroupids str_replace("$aw2gr"""$membergroupids);
                if (
$membergroupids != "")
                {
                    
$membergroupids $membergroupids $withcomma;
                }
                else
                {
                    
$membergroupids $aw2gr;
                }
            }
            else
            {
                
$membergroupids $aw2gr;
            }
            echo 
"UserID=".$giveawardsrow['userid']." Membergroups=".$membergroupids;
            
$userdm->set('membergroupids'$membergroupids);
            if (
count($userdm->errors))
            {
                for(
$i=0$i<count($userdm->errors); $i++)
                {
                    echo 
"ERROR{$i}:{$userdm->errors[$i]}\n";
                }
            }
            else
            {
                
// If everything is OK
                
$userdm->save();
                echo 
" | Account updated!";
                
$userdm->set_existing($userinfo);
                
$membergroupids $userdm->fetch_field('membergroupids');
                echo 
" | UserID=".$giveawardsrow['userid']." Membergroups=".$membergroupids;
            }
        }
        
$vbulletin->db->free_result($giveawardsrow);
    }

Reply With Quote
 


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:47 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.04702 seconds
  • Memory Usage 2,994KB
  • Queries Executed 12 (?)
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)bbcode_code
  • (13)bbcode_php
  • (3)bbcode_quote
  • (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
  • (8)postbit_onlinestatus
  • (9)postbit_wrapper
  • (1)showthread_list
  • (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_threadedmode.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_threaded
  • showthread_threaded_construct_link
  • 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