Author Topic: How can I show the name of the user?  (Read 15366 times)

Macchiato

  • Beginner
  • *
  • Posts: 43
How can I show the name of the user?
« on: December 16, 2010, 15:22:39 pm »
I wrote the following lines in index.php:

Code: [Select]
<div id="hello">
  <?php if ($this->countModules('user5')) { ?>
    <div id="hello-registered">
      <h3>Hello <?php $user = & JFactory::getUser();$user->get('username');?>!</h3>
    </div>
  <?php ?>
</div>

My goal is to show a greeting message and the name of the user when the user is logged in.
The greeting message "Hello" shows up when the user logs in, but the php line <?php $user = & JFactory::getUser();$user->get('username');?> doesn't print the name of the user.
I assumed <?php $user = & JFactory::getUser();$user->get('username');?> would print the name of the user, but now I'm not sure if it does. Am I using the wrong code for this or am I missing something? How can I show the name of the user?

P.S. I'm using the Ja Larix template.
Currently using Joomla 1.5.23 and VirtueMart 1.1.9

"Take a seat and have a coffee." -- Macchiato

Macchiato

  • Beginner
  • *
  • Posts: 43
Re: How can I show the name of the user?
« Reply #1 on: February 04, 2011, 16:37:25 pm »
I finally got it!  :D

The following code will display the first and the last name of the customer:

Code: [Select]
<?php $user =& JFactory::getUser(); echo $user->name;?>
The only thing I need to know now is how to only display the first name... anyone know how to do this?
Currently using Joomla 1.5.23 and VirtueMart 1.1.9

"Take a seat and have a coffee." -- Macchiato

zanardi

  • Contributing Developer
  • Full Member
  • *
  • Posts: 878
    • GiBiLogic
Re: How can I show the name of the user?
« Reply #2 on: February 04, 2011, 18:16:39 pm »
JFactory->getUser is a Joomla method, not Virtuemart. In Joomla, you don't have first name and second name.

If you want to access Virtuemart user data you use ps_user class instead.
--
Francesco (zanardi)
http://extensions.gibilogic.com
@gibilogic on Twitter

Macchiato

  • Beginner
  • *
  • Posts: 43
Re: How can I show the name of the user?
« Reply #3 on: February 05, 2011, 19:45:46 pm »
How do I use ps_user to display the first name? Can you give me an example?
Currently using Joomla 1.5.23 and VirtueMart 1.1.9

"Take a seat and have a coffee." -- Macchiato

zanardi

  • Contributing Developer
  • Full Member
  • *
  • Posts: 878
    • GiBiLogic
Re: How can I show the name of the user?
« Reply #4 on: February 06, 2011, 13:27:37 pm »
You can find some example in ps_checkout.php: search for

Code: [Select]
require_once( CLASSPATH . 'ps_user.php' );

and watch closely following lines.
--
Francesco (zanardi)
http://extensions.gibilogic.com
@gibilogic on Twitter

Macchiato

  • Beginner
  • *
  • Posts: 43
Re: How can I show the name of the user?
« Reply #5 on: February 07, 2011, 10:25:42 am »
This is what I made with your directions:
Code: [Select]
<?php require_once( CLASSPATH 'ps_user.php' );$dbu =& ps_user::getUserInfo(); echo $dbu->first_name;?>
Only it didn't work...

I can't seem to figure out what the proper line is to make this work  ???
Currently using Joomla 1.5.23 and VirtueMart 1.1.9

"Take a seat and have a coffee." -- Macchiato

zanardi

  • Contributing Developer
  • Full Member
  • *
  • Posts: 878
    • GiBiLogic
Re: How can I show the name of the user?
« Reply #6 on: February 07, 2011, 11:23:44 am »
If you look at ps_user::getUserInfo(), you'll see it requires a $userid parameter. While JFactory::getUser() retrieves who's the CURRENT LOGGED IN user, ps_user::getUserInfo() only retrieves additional info about a specific user id.

Wait a minute! What did i just say?
JFactory::getUser() get CURRENT LOGGED IN user.
ps_user::getUserInfo() get additional user info about a SPECIFIC USER ID.
Hmm... i can see a solution there... do you see it?
--
Francesco (zanardi)
http://extensions.gibilogic.com
@gibilogic on Twitter

Macchiato

  • Beginner
  • *
  • Posts: 43
Re: How can I show the name of the user?
« Reply #7 on: February 07, 2011, 17:46:06 pm »
So you say I need to use getUser instead of getUserInfo, because getUser grabs the CURRENT LOGGED IN user, and I need to use the $userid parameter instead of $dbu?
This is what I got so far:
Code: [Select]
<?php $userid =& ps_user::getUser(); echo $userid->first_name;?>
But this doesn't work  :(

Do I still need to use require_once( CLASSPATH . 'ps_user.php' ) to access vm's user data?

This is all very puzzling for me, remember I have a very very basic skill in this  :-\
Currently using Joomla 1.5.23 and VirtueMart 1.1.9

"Take a seat and have a coffee." -- Macchiato

zanardi

  • Contributing Developer
  • Full Member
  • *
  • Posts: 878
    • GiBiLogic
Re: How can I show the name of the user?
« Reply #8 on: February 08, 2011, 09:23:31 am »
Quote
This is all very puzzling for me, remember I have a very very basic skill in this

Then improve your skill :-)

You cannot use a method from one class in another class. My suggestion was to use both.

FIRST, get current user

Code: [Select]
$current_user = JFactory::getUser();

THEN, get user info

Code: [Select]
$userinfo = ps_user::getUserInfo( $current_user->id );

If this still does not make sense to you, you are really trying something too difficult for your current skill.
--
Francesco (zanardi)
http://extensions.gibilogic.com
@gibilogic on Twitter

expertslogic

  • Beginner
  • *
  • Posts: 5
Re: How can I show the name of the user?
« Reply #9 on: June 22, 2011, 08:21:05 am »
I used this code to get "First Name" of VirtueMart User.

$current_user = JFactory::getUser();
$userinfo = ps_user::getUserInfo($current_user->id);
$firstname=$userinfo->f("first_name");

This shall work for you too.

Thanks,
Q. J.

ryandiaz

  • Beginner
  • *
  • Posts: 18
  • VirtueMart Version: 1.1.6 Stable
Re: How can I show the name of the user?
« Reply #10 on: July 12, 2011, 04:12:14 am »
Geez, finally someone was nice enough to answer the question directly.  What's with all these tiny hints and points in the right direction?  I know the whole "give a man a fish / teach a man to fish" thing, but c'mon, just paste the code if you know it, and shut up if you don't.

 :P
Joomla 1.5.22 | Virtuemart 1.1.6 Stable | PHP 5.3.13

ryandiaz

  • Beginner
  • *
  • Posts: 18
  • VirtueMart Version: 1.1.6 Stable
Re: How can I show the name of the user?
« Reply #11 on: July 06, 2013, 07:34:21 am »
I left this project of displaying the currently logged in user's first name alone.  But now I'm coming back to it. 

Using Joomla v. 1.5.22 and Virtuemart v. 1.1.6 stable, the following works:

  <?php 
   require_once( CLASSPATH . 'ps_user.php' );
   $current_user =& JFactory::getUser();
   $userinfo =& ps_user::getUserInfo($current_user->id);
   $firstname =& $userinfo->f("first_name");
   echo $firstname;
  ?>

I currently have mine inside this file:
/shop/modules/mod_virtuemart/mod_virtuemart.php
right above the mini cart. I am going to make it say "Hello, Ryan" when a user is logged in, but say "Hello Guest, Login here" when logged out.

I will report back my complete code if I get the latter part working.
Joomla 1.5.22 | Virtuemart 1.1.6 Stable | PHP 5.3.13