2013-07-29 34 views
0

我正在使用以下內容來獲取打印在標籤上的用戶的數據。在PHP中使用Joomla 2.5 user_profile字段

$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

回答

0

使用

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

獲得用戶配置文件的所有字段爲特定的用戶。然後使用作爲

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

訪問所需的字段。

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 
+0

真的不能拯救我的任何工作。 – ChuckO