Using PHP code to retrieve current user name?

  • John Waraksa
  • Topic Author
  • Offline
  • New Member
  • New Member
More
8 years 1 month ago #79666 by John Waraksa
Using PHP code to retrieve current user name? was created by John Waraksa
I attempted to use this PHP code to get the name of user logged into site:

//<code>
$db =& JFactory::getDBO();
$db->setQuery("SELECT last_name FROM syx8v_osmembership_subscribers");
$result = $db->loadResult();
return $result;
//</code>

This only retrieves the very first user name in database, not the current user logged into site. Any suggests how to get last name of user logged into site automatically into renewal form?

Please Log in or Create an account to join the conversation.

  • Tuan Pham Ngoc
  • Away
  • Administrator
  • Administrator
More
8 years 1 month ago #79684 by Tuan Pham Ngoc
Replied by Tuan Pham Ngoc on topic Using PHP code to retrieve current user name?
Hi John

Your code is not correct. Please try this one:
Code:
$user = JFactory::getUser(); $db = JFactory::getDBO(); $db->setQuery("SELECT last_name FROM syx8v_osmembership_subscribers WHERE user_id=".$user->id); $result = $db->loadResult();

That should work OK

Tuan

Please Log in or Create an account to join the conversation.

  • John Waraksa
  • Topic Author
  • Offline
  • New Member
  • New Member
More
8 years 1 month ago #79790 by John Waraksa
Replied by John Waraksa on topic Using PHP code to retrieve current user name?
I attempted this code without proper results. No error code message; however, input text field on form was just blank. Recently, I switched to PHP7, so not sure if this is reason for code not working.

I attempted to modify this code without success. I would appreciate any other suggestions.

Thank you,
John

Please Log in or Create an account to join the conversation.

  • Tuan Pham Ngoc
  • Away
  • Administrator
  • Administrator
More
8 years 1 month ago #79839 by Tuan Pham Ngoc
Replied by Tuan Pham Ngoc on topic Using PHP code to retrieve current user name?
Hi John

Actually, I don't understand what form you are talking about. The code itself is correct, not sure how you put that data to the form.

Could you please explain more details so that I can guide you? Maybe you can submit a support ticket so that we can check the code for you?

Regards,

Tuan

Please Log in or Create an account to join the conversation.

  • John Waraksa
  • Topic Author
  • Offline
  • New Member
  • New Member
More
8 years 1 month ago #79858 by John Waraksa
Replied by John Waraksa on topic Using PHP code to retrieve current user name?
Oh, perhaps what I am attempting is not possible. Let me try to explain in more detail.

I am using another plugin for forms called RSForm! Pro (from RSJoomla!). When building forms, this software allows putting in PHP code for default text Input Fields. I just assumed it would be easy to grab information from backend database about person logged into OSSolution Membership Pro.

Maybe another approach would be to grab info from Joomla itself. Perhaps I can also get help from RSJoomla! forum. I really do appreciate your quick responses.

Thank you!

Please Log in or Create an account to join the conversation.

More
8 years 1 month ago #79864 by Calum
if you are retrieving the current user from JFactory with get user then provided the data is in the Joomla Core table you can do it with less code:
Code:
$user = JFactory::getUser(); if (!$user->guest) { echo 'You are logged in as:<br />'; echo 'User name: ' . $user->username . '<br />'; echo 'Real name: ' . $user->name . '<br />'; echo 'User ID : ' . $user->id . '<br />'; }

But not sure why the example give before doesn't work - its not a wrapping issue as the SQL select splits over two lines on my screen?

Please Log in or Create an account to join the conversation.

  • John Waraksa
  • Topic Author
  • Offline
  • New Member
  • New Member
More
8 years 1 month ago #79869 by John Waraksa
Replied by John Waraksa on topic Using PHP code to retrieve current user name?
Yes, your original PHP code was correct but missing one required line:
return $result;

So complete functioning code for RSForm! Pro is:

//<code>
$user = JFactory::getUser();
$db = JFactory::getDBO();
$db->setQuery("SELECT last_name FROM syx8v_osmembership_subscribers WHERE user_id=".$user->id);
$result = $db->loadResult();
return $result;
//</code>

Again, thank you for your help as I probably would have never figured out a working PHP code.

Please Log in or Create an account to join the conversation.

More
8 years 1 month ago #79870 by Alan Bennett
Replied by Alan Bennett on topic Using PHP code to retrieve current user name?
I find the best way to work out the code I need is to look at the source code in the extensions you have added to your system plus the joomla help pages.

Please Log in or Create an account to join the conversation.

  • Tuan Pham Ngoc
  • Away
  • Administrator
  • Administrator
More
8 years 1 month ago #79895 by Tuan Pham Ngoc
Replied by Tuan Pham Ngoc on topic Using PHP code to retrieve current user name?

John Waraksa wrote: Yes, your original PHP code was correct but missing one required line:
return $result;

So complete functioning code for RSForm! Pro is:

//<code>
$user = JFactory::getUser();
$db = JFactory::getDBO();
$db->setQuery("SELECT last_name FROM syx8v_osmembership_subscribers WHERE user_id=".$user->id);
$result = $db->loadResult();
return $result;
//</code>

Again, thank you for your help as I probably would have never figured out a working PHP code.


As I don't know how rsform works, I could only give you the basic code you need. Glad to hear that you figure it out

Tuan

Please Log in or Create an account to join the conversation.