• Resolved Roelf Keulder

    (@roelfk7)


    Good day all, Please help.

    I try to change a list of info where it currently show all the user IDs, it should show “you” for the current logged in user in the list.
    So I’m trying to use the if else statement but it does not show the user as you.

    Please see my code before the change: <?php echo $result->id; ?> This is working correctly and shows all the users IDs.
    Here i is with my if else statement: if ($user = $result->id){
    <?php echo You ?>;}
    else {
    <?php echo $result->id; ?> }
    I have tried it with and without the {} brackets. I have even tried a JavaScript with the if else statement. And I also tried adding this $user = wp_get_current_user(); before the list, but it wont work.

    Here is the output I get:

    if ($user = $result->id){
    Notice: Use of undefined constant You - assumed 'You' in 
    You} else { 39 }
    R5,000.00
    2 days ago
    if ($user = $result->id){
    Notice: Use of undefined constant You - assumed 'You' in 
    You} else { 38 }
    R4,000.00
    2 days ago

    It is suppose to look like this

    39
    R5,000.00
    2 days ago
    38
    R4,000.00
    2 days ago
    37
    R2,500.00
    2 days ago

    Any help will be appreciated.

    • This topic was modified 3 years, 2 months ago by Roelf Keulder.

    The page I need help with: [log in to see the link]

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hi @roelfk7,

    I think you forgot to add quotes before and after ‘You’ and a semicolon ; before the closing php tag ?>.

    It should be <?php echo 'You'; ?>

    Thanks,
    M

    • This reply was modified 3 years, 2 months ago by Mihail Vasilchenko. Reason: added code tags
    Moderator bcworkz

    (@bcworkz)

    Also, you have if ($user = $result->id). While this may do what you want, you are also inadvertently assigning the value of $result->id to $user, which I doubt is your intent. To check equivalency without assignment, use the == operator instead of =. You could also verify type matching with ===. Usually not that important, but sometimes it’s crucial, for example
    0 == false returns true, but
    0 === false returns false. This can make a huge difference in functions like strpos()

    Thread Starter Roelf Keulder

    (@roelfk7)

    @bcworkz Thank you so much for the quick reply I will try it and get back to Thank you also for the explanation of when to use =, == and ===

    Thread Starter Roelf Keulder

    (@roelfk7)

    Thank you @mafnah will try it thank you for the quick response

    Thread Starter Roelf Keulder

    (@roelfk7)

    Thank you both. I have tried your suggestions and now it returns the following:

    if ($current_user->user_login == $result->name){ You} else { 40 }
    R16,000.00
    38 minutes ago

    My code now looks like this:
    <?php
    $current_user = wp_get_current_user(); ?>
    if ($current_user->user_login == $result->name){
    <?php echo 'You' ?>}
    else {
    <?php echo $result->id; ?> }

    Thread Starter Roelf Keulder

    (@roelfk7)

    Thank you @mafnah and @bcworkz, you are awesome I got it to work

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Check current login user’ is closed to new replies.