繁体   English   中英

在PHP中使用Joomla 2.5 user_profile字段

[英]Using Joomla 2.5 user_profile fields in PHP

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

$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

您可以向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

采用

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

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

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

访问必填字段。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM