How to get currently logged in user data at vewlisting.tpl?

Discussion in 'Technical Support' started by user2616, Dec 28, 2011.

  1. user2616 Customer

    I want to get currently logged in user data at vewlisting.tpl

    Any help is appreciated.

    Thanks you.
    Peri
  2. seymourjames All Hands On Deck

    It depends upon what data?

    You can use the debug functionality in smarty to see what variables are available to that template file. If you wish to restrict that information to members only (i.e. those which are logged in), you would display them in a simple test. Something like


    {if $smarty.session.username<>'' || $smarty.cookies.username<>""}

    The variables you wish to display

    {/if}

    If the variables do not exist you will need to engage in core file changes.
  3. user2616 Customer

    Thanks David for your quick reply.

    I want the address of logged in user.

    I enabled {debug}

    Found the following variables relevant to address:
    {$address} - Listing user address that I do not want.
    {$b_address} - Billing address of the listing user that I do not want.
    {$session} - Long string
    {$cookie} - Long string

    I cannot find any variable related to logged in user.

    Can you please give me some idea on how to access logged in user data.
  4. seymourjames All Hands On Deck

    So let me understand. If I am logged in as Seymour-James you want my physical address to appear on my adverts or on all adverts?

    Are you actually trying to prefill a contact form by any chance?
  5. seymourjames All Hands On Deck

    If you want the address of the person logged in then you will need to make a core file change to viewlisting.php or make a plugin which you can call from within viewlisting.tpl. The code you will need is basically in useraccountmodify.php .


    I will look at what needs to be done
  6. user2616 Customer

    Basically, my requirement is very simple.

    Firstly, I answer your questions.

    Q: If I am logged in as Seymour-James you want my physical address to appear on my adverts or on all adverts?
    A: No.

    Q: Are you actually trying to prefill a contact form by any chance?
    A: No.

    Now, I tell you what I refer as address of logged in user.
    Login > My Account > Modify User Account
    User Registration (useraccountmodify.tpl) page appear.
    Fields such as First Name, Last Name, Email Address, Address are displayed.
    I am referring to the field Address above.

    Let us say you (Seymour-James) logged in.
    Click My Account
    Click Modify User Account
    User Registration (useraccountmodify.tpl) page appar.
    Key in "some text" in Address field.
    Click Submit.

    You select a listing and and go to View Listing (viewlisting.tpl) page.
    Let us say this listing is added by the user Peter.
    At this point of time, I want to access the Address (value = "some text") of logged in user Seymour-James.
    Not the Address of the listing user Peter.

    My question: How to get the value of the field Address of logged in user in viewlisting.tpl?

    Thanks.
    Peri
  7. seymourjames All Hands On Deck

    Yes I understood. It is not so simple. I am looking at it now.
  8. seymourjames All Hands On Deck

    Try this in viewlisting.php at line 21 - just after the closing brace and before $class_tpl->assign...


    if (!empty($_SESSION['uid']))
    {
    $sql = "SELECT firstname, lastname, username, address, lastname, address, city, state, zip, country FROM ".PREFIX."users WHERE id=".(int)$_SESSION['uid'];
    $result = $db->query($sql);
    if ($result->size() > 0)
    {
    $user_data = $result->fetch();
    $class_tpl->assign('logged_user', $user_data);
    $class_tpl->assign('useraddress', $user_data[address]);
    // add in more queires to pass if you wish
    }
    }


    Then in your viewlisting.tpl file

    {if $smarty.session.username<>'' || $smarty.cookies.username<>""}

    {$useraddress}
    // add in more if you wish

    {/if}
  9. user2616 Customer

    Thank you for leading me to the solution that I already was using in showlisting2.tpl, but I never realized until you drag me to the point with your nice sample code and idea that opens my eyes and mind to understand how to solve my problem.

    showlisting2.tpl is calling a plugin to get listing user's name and phone.
    I just changed the plugin to add address field.

    Inserted the following code in viewlisting.tpl

    {if $smarty.session.uid > 0}
    {get_user_field userid=$smarty.session.uid}
    {elseif $smarty.cookies.uid > 0}
    {get_user_field userid=$smarty.cookies.uid}
    {else}
    {$u_address = ''}
    {/if}
    {$u_address}

    It simply works. It is really fantastic.

    Seymour-James, I really want to thank you for your time and effort to understand my problem and provide a solution.

    Now, my problem solved.

    But, I have couple of small questions to you:

    1. I am not defining the variable $u_address in viewlisting.tpl, but smarty is not giving error at {else} {$u_address = ''} {/if}. Why?

    2. You mentioned '' (2 single quotes) and "" (2 double quotes) in {if $smarty.session.username<>'' || $smarty.cookies.username<>""}. How to know when to use '' (2 single quotes) and "" (2 double quotes)?

    Thanks.
    Peri
  10. seymourjames All Hands On Deck

    You can't generally test the output value of the plugin in the template file (do the test inside of the plugin). Same issue with the get extra fields plugin (see various other forum posts about that). I would just use the code that has been given. It works.

    No real issues or rule if you use double or single quotes. Just how it was coded in the original default template.
  11. user2616 Customer

    My existing plugin works fine for more than 1 year to get listing user data.

    But, for the logged in user, the status can be either logged in or not logged in. Your following if statement takes care of this condition very well. I know you guys think better as developers.

    {if $smarty.session.username<>'' || $smarty.cookies.username<>""}

    I notice that when logged in, only $smarty.session.username contains the value of username , but $smarty.cookies.username contains "". When not logged in, both $smarty.session.username and $smarty.cookies.username contains "".

    My question: When does $smarty.cookies.username works?

    Thanks.
    Peri
  12. Mike-N-Tosh Owner

    When you check "Remember me" in the login
  13. user2616 Customer

    Are you referring to "Remember My ID" just above Submit button in at login page?

    I clicked "Remember My ID" when I logged in.
    $smarty.session.uid contains the logged in user id.
    $smarty.cookies.uid contains null.

    Peri
  14. John Snyder Staff

    User id is never set in a cookie. Username is though.
  15. seymourjames All Hands On Deck

    The test is the same one used in the default layout template for the welcome user.
  16. Mike-N-Tosh Owner

    My answer:
    When you check "Remember me" in the login

    Why do you ask one question, then someone gives you the answer to your question and you want to argue that it doesn't referring to something completely different?

  17. Mike-N-Tosh Owner

    Perhaps if you stated what your actual goal is here and your reasoning as to why you want this it would help.

    I don't understand at all what purpose showing the logged in users information to the logged in user him/herself serves when looking at someone else's listing. Doesn't the logged in user already know where they live?

    What are you actually trying to achieve?
  18. seymourjames All Hands On Deck

    "Ours is not to reason why. Ours is but to do and die" . All part of the education on the 68C forum..
  19. user2616 Customer

    Mike,
    Sorry for the confusion.
    I realized my mistake that my question was on $smarty.cookies.username, but I jumped to $smarty.session.uid

    My actual goal is "how to to get currently logged in user data at vewlisting.tpl"
    And I got the solution already.

    Your question on Address make sense.
    But, I am using Address field as custom field.

    Background:
    I already used up 3 extra fields in user record.
    But, I want one more extra field to store custom data.
    I was not using Address field.
    So, I renamed Address field as a custom field name in english.php
    I want to use this custom field in viewlisting.tpl
    But, I got stuck here.
    Instead of telling this story, I just asked a simple & direct question How to get currently logged in user data at vewlisting.tpl?
    And I got the solution.

    Again sorry for the confusion.

    John,
    Currently, I constructed my if statement as follows:

    {if $smarty.session.uid > 0}
    {get_user_field userid=$smarty.session.uid}
    {elseif $smarty.cookies.uid > 0}
    {get_user_field userid=$smarty.cookies.uid}
    {else}
    {$u_address = ""}
    {/if}

    You mentioned that User id is never set in a cookie.

    That means, the following statement make no sense. Am I right?

    {elseif $smarty.cookies.uid > 0}
    {get_user_field userid=$smarty.cookies.uid}

    So can I construct my if statement as follows?

    {if $smarty.session.uid > 0} {* when the user is logged in *}
    {get_user_field userid=$smarty.session.uid} {* get user data using uid *}
    {else}
    {$u_address = ""} {* else assign null *}
    {/if}

    If my above if statement is not correct, please suggest me a meaningful one.

    Thank you all for your help.

    Peri

Share This Page