Search code examples
phparrayswordpressbuddypress

Array variable into another function variable


I have the following PHP function to fetch some user_ids which I would then like to add as recipients to a message as per below.

function true_my_bp_get_users_by_xprofile( $field_id_to_check, $num_to_find ) {
global $wpdb;


$table_name = $wpdb->prefix . "bp_xprofile_data";

$user_ids = $wpdb->get_results( 
     $wpdb->prepare( 
        "SELECT user_id FROM $table_name 
                 WHERE field_id = %d AND value LIKE '%%\"%d\"%%'", 
        $field_id_to_check,
        $num_to_find
    )
);
print_r($user_ids);
}

I am using true_my_bp_get_users_by_xprofile( 5, 18 ); which prints Array ( [0] => stdClass Object ( [user_id] => 1 ) [1] => stdClass Object ( [user_id] => 2 ) )

Then I have a HTML form with this code:

$body_input=isset($_POST['body_input'])?$_POST['body_input']:'';
$subject_input=isset($_POST['subject_input'])?$_POST['subject_input']:'';

send_msg( $user_ids,$body_input, $subject_input);

With send_msg being

function send_msg($user_id, $title, $message){
$args = array( 'recipients' => $user_id, 'sender_id' => bp_loggedin_user_id(), 'subject' => $title, 'content' => $message );
messages_new_message( $args );
}

What I want to do:

Take the array from $user_ids and put it here: 'recipients' => $user_id

I have tried replacing $user_id with $user_ids in the function but it doesn't work.


Solution

  • Since you are putting data in the $user_ids variable inside a function, its scope is limited to that function only. The data can be stored and accessed outside the function in a couple of differetn ways.

    1). Pass a variable to true_my_bp_get_users_by_xprofile by reference.

    $user_ids = null;
    
    function true_my_bp_get_users_by_xprofile( $field_id_to_check, $num_to_find, &$user_ids ) {
        global $wpdb;
        $table_name = $wpdb->prefix . "bp_xprofile_data";
    
        $user_ids = $wpdb->get_results( 
             $wpdb->prepare( 
                "SELECT user_id FROM $table_name 
                         WHERE field_id = %d AND value LIKE '%%\"%d\"%%'", 
                $field_id_to_check,
                $num_to_find
            )
        );
        print_r($user_ids);
    }
    

    Call the function

    true_my_bp_get_users_by_xprofile( 5, 18, $user_ids );
    

    Now your $user_ids has the data and accessable outside the function.

    2). Return the $user_ids from true_my_bp_get_users_by_xprofile function

    function true_my_bp_get_users_by_xprofile( $field_id_to_check, $num_to_find ) {
        global $wpdb;
        $table_name = $wpdb->prefix . "bp_xprofile_data";
    
        $user_ids = $wpdb->get_results( 
             $wpdb->prepare( 
                "SELECT user_id FROM $table_name 
                         WHERE field_id = %d AND value LIKE '%%\"%d\"%%'", 
                $field_id_to_check,
                $num_to_find
            )
        );
        print_r($user_ids);
    
        return $user_ids;
    }
    

    Calling the function like $user_ids = true_my_bp_get_users_by_xprofile( 5, 18 );

    Now, you can call the send_msg function as you've done in your code above i.e.

    send_msg( $user_ids, $body_input, $subject_input);