Forums

Help with code

cymak 20 Aug, 2015
I am setting user groups using your example and it works fine when inputting static values


<?php
if ( !isset($form->data['OrgID']) || !$form->data['OrgID'] ) {
  // there is no division selected
  return false;
}

If ( $form->data['UserType'] = 1 ){
$group_array = array (
'1' => '29',
'2' => '33',
'5' => '47',
'4' => '29',
'9' => '27',
'6' => '22',
);
}
$group_id = $group_array[$form->data['OrgID']];
$user_id  = $form->data['_PLUGINS_']['joomla_registration']['id'];
jimport( 'joomla.user.helper' );
JUserHelper::addUserToGroup($user_id, $group_id);
?>


But when I try to pull the array data it just gives me an incorrect usergroup error, I am tryin to replace


$group_array = array (
'1' => '29',
'2' => '33',
'5' => '47',
'4' => '29',
'9' => '27',
'6' => '22',
);


with something like below, OrgData is the Model ID from a read db in Load


$group_array = array ();
foreach ( $form->data['OrgData'] as $d ) {
    $group_array[$d['ID']] =  $d['DancerGP'];
 }


I know its something small but not sure what, can someone help? Thanks!!
GreyHead 21 Aug, 2015
Hi cymak,

Please drag a Debugger action into the On Submit event, then submit the form and post the debug results here so we can see what is in the $form->data['OrgData'] array,

Bob
cymak 21 Aug, 2015
Hi Here is the code, I have changed the Model ID to GetOrg2, Thanks for your help!

<?php
if ( !isset($form->data['OrgID']) || !$form->data['OrgID'] ) {
  // there is no division selected
  return false;
}

If ( $form->data['UserType'] = 1 ){
$group_array = array ();
foreach ( $form->data['OrgData2'] as $d ) {
    $group_array[$d['ID']] =  $d['DancerGP'];
 }
}
$group_id = $group_array[$form->data['OrgID']];
$user_id  = $form->data['_PLUGINS_']['joomla_registration']['id'];
jimport( 'joomla.user.helper' );
JUserHelper::addUserToGroup($user_id, $group_id);
?>


and the debug

Notice: Undefined index: OrgData2 in /home/feisonli/public_html/administrator/components/com_chronoforms5/chronoforms/actions/custom_code/custom_code.php(20) : eval()'d code on line 9

Warning: Invalid argument supplied for foreach() in /home/feisonli/public_html/administrator/components/com_chronoforms5/chronoforms/actions/custom_code/custom_code.php(20) : eval()'d code on line 9
×Thank you! Your account has been created and an activation link has been sent to the email address you entered. Note that you must activate the account by clicking on the activation link when you get the email before you can login.
Data Array
Array
(
    [chronoform] => PreRegister3
    [event] => submit
    [UserType] => 1
    [OrgID] => 9
    [SchID] => 12
    [Name] => fgjgf
    [Email] => gfhjgh@fgjg.ie
    [Username] => sdgsddf
    [Password] => as
    [RePassword] => as
    [button5] => Submit
    [user_id] => 100
    [Org_Name] => 
    [School_Name] => 
    [name] => fgjgf
    [username] => sdgsddf
    [email] => gfhjgh@fgjg.ie
    [password] => as
    [password2] => as
    [_PLUGINS_] => Array
        (
            [joomla_registration] => Array
                (
                    [*isRoot] => 
                    [id] => 100
                    [name] => fgjgf
                    [username] => sdgsddf
                    [email] => gfhjgh@fgjg.ie
                    [password] => $2y$10$S8n6p8vTJnpbD492jnNMFuTcOg/SgNqGEsAq92BDeBxy9LhOQLnGG
                    [password_clear] => as
                    [block] => 1
                    [sendEmail] => 0
                    [registerDate] => 2015-08-21 05:24:08
                    [lastvisitDate] => 
                    [activation] => 597ff112e87135bf729d16178960ead7
                    [groups] => Array
                        (
                            [0] => 2
                        )

                    [lastResetTime] => 
                    [resetCount] => 
                    [requireReset] => 
                    [*_params] => Joomla\Registry\Registry Object
                        (
                            [data:protected] => stdClass Object
                                (
                                )

                            [separator] => .
                        )

                    [*_authGroups] => Array
                        (
                            [0] => 1
                            [1] => 9
                        )

                    [*_authLevels] => Array
                        (
                            [0] => 1
                            [1] => 1
                            [2] => 5
                        )

                    [*_authActions] => 
                    [*_errorMsg] => 
                    [*userHelper] => JUserWrapperHelper Object
                        (
                        )

                    [*_errors] => Array
                        (
                        )

                    [aid] => 0
                    [chronoform] => PreRegister3
                    [event] => submit
                    [UserType] => 1
                    [OrgID] => 9
                    [SchID] => 12
                    [Name] => fgjgf
                    [Email] => gfhjgh@fgjg.ie
                    [Username] => sdgsddf
                    [Password] => as
                    [RePassword] => as
                    [button5] => Submit
                    [user_id] => 
                    [Org_Name] => 
                    [School_Name] => 
                    [password2] => as
                    [usertype] => deprecated
                )

        )

    [GetData2] => Array
        (
            [0] => Array
                (
                    [ID] => 1
                    [Org_Name] => An Comhdhail
                    [DancerGP] => 15
                )

            [1] => Array
                (
                    [ID] => 2
                    [Org_Name] => An Comhdhail (USA)
                    [DancerGP] => 0
                )

            [2] => Array
                (
                    [ID] => 3
                    [Org_Name] => CLRG (Ireland)
                    [DancerGP] => 0
                )

            [3] => Array
                (
                    [ID] => 4
                    [Org_Name] => CLRG (GB)
                    [DancerGP] => 0
                )

            [4] => Array
                (
                    [ID] => 5
                    [Org_Name] => CLRG (USA)
                    [DancerGP] => 0
                )

            [5] => Array
                (
                    [ID] => 6
                    [Org_Name] => CLRG (Canada)
                    [DancerGP] => 0
                )

            [6] => Array
                (
                    [ID] => 7
                    [Org_Name] => CLRG (Australia)
                    [DancerGP] => 0
                )

            [7] => Array
                (
                    [ID] => 8
                    [Org_Name] => CLRG (Others)
                    [DancerGP] => 0
                )

            [8] => Array
                (
                    [ID] => 9
                    [Org_Name] => CRN
                    [DancerGP] => 27
                )

            [9] => Array
                (
                    [ID] => 10
                    [Org_Name] => CRN (USA)
                    [DancerGP] => 0
                )

            [10] => Array
                (
                    [ID] => 11
                    [Org_Name] => WIDA
                    [DancerGP] => 0
                )

            [11] => Array
                (
                    [ID] => 12
                    [Org_Name] => CRDM
                    [DancerGP] => 0
                )

            [12] => Array
                (
                    [ID] => 13
                    [Org_Name] => RTN
                    [DancerGP] => 0
                )

            [13] => Array
                (
                    [ID] => 14
                    [Org_Name] => CAID
                    [DancerGP] => 0
                )

            [14] => Array
                (
                    [ID] => 15
                    [Org_Name] => CMFA
                    [DancerGP] => 0
                )

            [15] => Array
                (
                    [ID] => 16
                    [Org_Name] => CRG
                    [DancerGP] => 0
                )

            [16] => Array
                (
                    [ID] => 17
                    [Org_Name] => CIDA
                    [DancerGP] => 0
                )

            [17] => Array
                (
                    [ID] => 18
                    [Org_Name] => FDTA
                    [DancerGP] => 0
                )

            [18] => Array
                (
                    [ID] => 19
                    [Org_Name] => Other
                    [DancerGP] => 0
                )

        )

    [ID] => 14
)
Array
(
)
Errors
Array
(
)
Debug Info
Array
(
    [42] => Array
        (
            [DB Read] => Array
                (
                    [Queries] => Array
                        (
                            [0] => SELECT `GetData2`.`ID` AS `GetData2.ID`, `GetData2`.`Org_Name` AS `GetData2.Org_Name`, `GetData2`.`DancerGP` AS `GetData2.DancerGP` FROM `Mak_z_Organization` AS `GetData2`
                        )

                )

        )

    [36] => Array
        (
            [DB Save] => Array
                (
                    [Queries] => Array
                        (
                            [0] => INSERT INTO `Mak_z_user` (`OrgID`, `SchID`, `user_id`, `Org_Name`, `School_Name`) values ('9', '12', '100', '', '');
                        )

                )

        )

)
GreyHead 21 Aug, 2015
Answer
Hi cymak,

The array name in the Debugger output is GetData2 so
$group_array = array ();
foreach ( $form->data['GetData2'] as $d ) {
    $group_array[$d['ID']] =  $d['DancerGP'];
 }

Bob
cymak 21 Aug, 2015
Arrrrggggg, when you originally replied I turned on the debugger and realized there was no array info and concluded that after submit the Read DB from the Load section must be gone(I was using the Model ID from the load section to add usergroup) and I had to add another read db after the submit before the adding or usergroup. It would have worked if I had just used the RIGHT Model ID!!! Im an idiot. Thanks alot for your help !!!🙂 Working now
This topic is locked and no more replies can be posted.