What's wrong with this code?

$recipient is brought through a form. This is my code...

<?php

echo $recipient;

$r1 = get_user_by_username($recipient);

$r2 = $r1->getGUID();
?>

and my result is just...

Speedysnail6

it is supposed to display the user guid next to it. What did I do wrong?

  • Maybe

    <?php

    if (elgg_is_logged_in()) {
            $user = elgg_get_logged_in_user_entity();

    }

    if ($user->isBanned()) {
            return null;
        }

    if (!$user->username) {

    elgg_echo ('go away');

    }

    else {

    elgg_echo ('hi');

    }

     

    Additional for using:

    $user_guid = $user->getGUID();

  • but what if the user doesn't exist?

    This is an action that gets the data from a textbox in a form.

  • $user = "Speedysnail6DisplayName";

    not get logged in user enity

  • What do u want to do?

     

        $r1 = get_user_by_username($recipient);
        
            $ra1 = get_user_by_username($recipient)->name;
        
        if ($recipient->name) {
        echo "hi";
        }
        echo $recipient;
        ?>


    In your code, $r1 will be the user entity, $ra1 will be the same as $recipient so will be the username.

    $recipient->name wont work because $recipient is just a string.
  • recepient is a username or display name entered from a form. I need to convert that username/display name into a user guid.

  • you can only get user by username or email, not disply name (its not unique).

    So just do:

    $r1 = get_user_by_username($recipient);

    echo $r1->getGUID();

    and that will print our user guid

  • I understand how to do it to display the username's guid. I need it to get the display name to show the guid too. Can it do it through getting the username of the display name then fetching the guid?

     

  • This is why...

    I am working on a site called "Jr. Coders" for young people to learn coding. There is a virtrual currency system (ElggX Userpoints) called "Credits". iionly created a plugin for me to exchange credits with another user for services, goods, etc. The recipent was a dropdown menu of all your friends. I need the user to enter a username/ Display Name instead.

    The points were sent with a user guid. I was able to convert the recipents username into a user guid with Sem's help. But, it is a lot easier to find out the Display Name of someone than the username.

  • As Mark said you can't get the user's GUID from the display name as the display name is not unique (basically every user on your site could choose the same display name).

    If you want users to select a recipient I would suggest to use a dropdown field and not a text input field. This allows to display the display names in the selection (if you want to) but you can evaluate the user's selection based on the username in the background. Also, you avoid users to enter a wrong name (either by mistake or on purpose).