Last updated 7 June 2014. Created on 16 November 2009.
Edited by acabouet, silverwing, inzzzomnia. Log in to edit this page.

There are times when you'd like to display information about the current logged in user. It could be a personalized greeting, a contact form where you don't want to make the user fill out fields with information that allready exists, whatever you name it!

Anyways, all information about your registerd users is stored in your database, it's just a matter of getting the information from it and on to your website.
All databases look a bit different depending on what fields you made the user fill out when creating an account.
To find out what's available to you, create a new php-code page and paste the following code:

<?php
  global $user;
  $clu = user_load(array('uid' => $user->uid));    //clu: Current Logged in User (something i made up, use whatever you whish!)
  print_r ($clu);
?>

Preview your page, the result should look something like this:

[field_user_retailer] => Company name Inc [field_user_firstname] => Peter [field_user_surname] => Smith [field_user_telephone] => 321-123 123[field_user_titel] => manager[field_user_type] => Array ( [0] => [1] => [2] => [3] => ) [field_user_homenumber] => 321-321 321 [field_user_cellphone] => 123-456 789 [field_user_private_email] => private_smith@somewhere.com

Ok, now we know where to go to access certain information. Use the info above to create somthing like:

<?php
  global $user;
  $clu = user_load(array('uid' => $user->uid));    //clu: Current Logged in User (something i made up, use whatever you whish!)
?>
Hello <?php echo $clu->field_user-firstname; ?>! Your private email is: <?php echo $clu->field_user_private_email; ?>.

which would output:

Hello Peter! Your private email is: private_smith@somewhere.com.

To make your coding more smooth, set it up like this:

<?php
  global $user;
  $clu = user_load(array('uid' => $user->uid));  //clu = Current Logged in User

  $firstname = &$clu->field_user_firstname;
  $surname = &$clu->field_user_surname;
  $companyname = &$clu->field_user_retailer;
  $privateemail = &$clu->field_user_private_email;
  ...and so on...
?>

Hello <?php echo $firstname  ?>! <br> Your private email is: <?php echo $privateemail; ?>.

I'm sure this can be done in a much better way. Anyway, i hope it helps someone.

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

marcoka’s picture

A good idea is to put such logic into your preprocess functions into the template.php

Example:

/**
 * Override or insert variables into the page templates.
 *
 * @param $vars
 *   An array of variables to pass to the theme template.
 * @param $hook
 *   The name of the template being rendered.
 */

function texturestar_preprocess_page(&$vars, $hook) {
  global $user;
  $clu = user_load(array('uid' => $user->uid));    //clu: Current Logged in User (something i made up, use whatever you whish!) 
  $vars['username'] = $clu->name;      
}

so you can use the variable $username in your page.tpl.php.

BobWebDesign’s picture

Thank you! this article has been really helpful to me!

manas_rocks’s picture

Its what i wanted ! thanx a lot !