I'm using the following to get the data for a user to be printed on a label.
$db = JFactory::getDBO();
$user =& JFactory::getUser();
$uid = $user->id;
$query = $db->getQuery(true);
$query
->select(array('user_id', 'profile_key', 'profile_value', 'ordering'))
->from('#__user_profiles')
->where('user_id LIKE $uid')
->order('ordering ASC');
$db->setQuery($query);
$result = $db->loadObjectList();
foreach ( $result as $row ) {
if ($row->profile_key == 'profile.firstname') $fname = $row->profile_value;
if ($row->profile_key == 'profile.middlename') $mname = $row->profile_value;
if ($row->profile_key == 'profile.lastname') $lname = $row->profile_value;
if ($row->profile_key == 'profile.address1') $aline1 = $row->profile_value;
if ($row->profile_key == 'profile.address2') $aline2 = $row->profile_value;
if ($row->profile_key == 'profile.city') $city = $row->profile_value;
if ($row->profile_key == 'profile.state') $state = $row->profile_value;
if ($row->profile_key == 'profile.postalcode') $zipcode = $row->profile_value;
}
Is there a simpler way of writing this code?
I'm using Joomla 2.5.11
You could pass loadObjectList
the value 'profile_key' which would key your array to the profile key so you could skip parsing it yourself:
$db = JFactory::getDBO();
$user =& JFactory::getUser();
$uid = $user->id;
$query = $db->getQuery(true);
$query
->select(array('user_id', 'profile_key', 'profile_value', 'ordering'))
->from('#__user_profiles')
->where('user_id LIKE $uid')
->order('ordering ASC');
$db->setQuery($query);
$result = $db->loadObjectList('profile_key');
$fname = $result['profile.firstname']->profile_value;
// no need to loop on this
// can just grab values from the array as needed
https://github.com/joomla/joomla-cms/blob/master/libraries/joomla/user/helper.php#L198
JUserHelper::getProfile() will help you out.
Use
$profile = JUserHelper::getProfile( $user->id );
to get all the fields of a user profile for a particular user. Then use as
$address1 = $profile->profile['address1'];
to access the required fields.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.