Вопрос

Im trying to modify this wordpress plugin of mine to track the name of who reffered the current logged user.

So far I managed to build the code below, but i'm very new with any kind of coding and I would be very thankfull if I get some help.

The idea is to get the "parent_user_id" (the referrer id) from the current user, them use this ID to get the login_ID.

Actualy this code is outputting this: "Referred by: Array"

function wpmlm_display_referrer() {
    global $wpdb, $user_ID, $current_user;
    if(isset($_GET['current_user_id']) && !empty($_GET['current_user_id'])) {
        $current_user_id = $_GET['current_user_id'];
    } else {
        $current_user_id = $current_user->ID;
    }

    # Logged in user
    if ( is_user_logged_in() == true ) {            

        $get_id = "SELECT parent_user_id FROM mlm WHERE user_id=%d".$current_user_id;
        $get_name = "SELECT login_id FROM mlm WHERE user_id=%d".$get_id;
        $ref = $wpdb->get_results($get_name);

        return 'Referred by: '.$ref;    
    }
}

Thank you for any help you can provide in this situation.

Это было полезно?

Решение

please try this:

$get_id = "SELECT parent_user_id FROM mlm WHERE user_id='".$current_user_id ."'";
$get_name = "SELECT login_id FROM mlm WHERE user_id='".$get_id ."'";

I have just edited the query. Now second thing is that $get_id is the variable containing the query so if you want to write the sub-query write it like below:

you below if parent_user_id from the sub query will return 1 value

$query = "SELECT login_id FROM mlm WHERE user_id=(SELECT parent_user_id FROM mlm WHERE user_id='".$current_user_id ."')";

use below if the sub query will return multiple values:

$query = "SELECT login_id FROM mlm WHERE user_id IN (SELECT parent_user_id FROM mlm WHERE user_id='".$current_user_id ."')";

and Then try print_r($ref);

you would get the value in the array. and once you see the value use the proper index of the array to get the value.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top