dp19001 2013-06-06 17:21
浏览 18
已采纳

尝试获取正在查看的个人资料的用户ID

The PHP script I am developing needs to pull a field value from the database based upon the profile being viewed at that time. Currently I have developed this script and it is functioning for me with the exception of one item. The problem is that the value it is displaying is based upon the user’s profile who is logged in rather than the profile being viewed. For example, let’s say that there is a field associated with each profile titled profile_key. The user who is logged in has the following value:

profile_key=123

When this user views their own profile, they see the following:

profile_key=123

Now, let’s continue this example with a second user. The second user (who is not logged in) has a key value of the following:

profile_key=789

Currently, when the logged in user (first user) visits the non-logged in user (second user), the logged in user sees the following displayed on the non-logged in user’s profile:

profile_key=123

The key value which should be display when the logged in user visits the non-logged in user’s profile is the value set for that user’s profile:

profile_key=789

This site is based in Joomla, and here is a snippet from the script which shows the query to obtain the profile key value:

// Get a db connection.
$db = JFactory::getDbo();
// Create a new query object.
$query = $db->getQuery(true);
// Get User ID
$user = JFactory::getUser();
$id = $user->id;
// Select all records from the user profile table where key begins with "custom.".
// Order it by the ordering field.
$query = 'SELECT cb_profilekey FROM #__comprofiler WHERE user_id = ' . $id.'';

As you can see, the query is pulling the value based upon the id of the user viewing the page rather than the is of the user page being viewed. This is where I need some adjustment I believe. Thank you for your time in this request, any assistance is much appreciated.

  • 写回答

1条回答 默认 最新

  • duanbage2161 2013-06-06 20:25
    关注
    $user = JFactory::getUser();
    

    this will always gives you the logged in user as object. you need to get the id of the profile, say from request like this.

    $id = JFactory::getApplication()->input->getInt('id', 0);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看