1

我正在使用以下内容获取要打印在标签上的用户数据。

$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;
}

有没有更简单的方法来编写这段代码?

我正在使用 Joomla 2.5.11

4

3 回答 3

0

您可以将loadObjectList值“profile_key”传递给配置文件键,这样您就可以跳过自己的解析:

$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
于 2013-07-29T21:24:50.027 回答
0

https://github.com/joomla/joomla-cms/blob/master/libraries/joomla/user/helper.php#L198

JUserHelper::getProfile() 会帮助你。

于 2013-07-30T01:22:15.587 回答
0

利用

$profile = JUserHelper::getProfile( $user->id );

获取特定用户的用户配置文件的所有字段。然后用作

$address1 = $profile->profile['address1'];

访问必填字段。

于 2015-01-16T02:14:15.203 回答