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

尝试获取正在查看的个人资料的用户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.

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