Find user by user_id

netkingZ

Active member
Hello ,
i need to get username from $user.user_id in template.
i thinks with foreach , how can do this?
Find user by id.

For example:
Code:
<xen:foreach loop="$user.user_id" value="99" i="$i">
<xen:if is="{$user.user_id} == '99'">
 {$user.username} <--- username of user_id 99
</xen:if>
</xenforeach>
thanks you
@Chris D @Mike
 

Robust

Well-known member
Why do you have a foreach loop there? From the small snippet you've shown, I don't believe you need it.

If you have a $user variable, chances are the whole user has been fetched, or at least enough data to also include the username. {$user.username} should show you the username. You do not need a foreach tag for this.

If you want to show the username only if the user ID is 99, then your code is correct if you remove the <xen:foreach> tags.
 

netkingZ

Active member
Why do you have a foreach loop there? From the small snippet you've shown, I don't believe you need it.

If you have a $user variable, chances are the whole user has been fetched, or at least enough data to also include the username. {$user.username} should show you the username. You do not need a foreach tag for this.

If you want to show the username only if the user ID is 99, then your code is correct if you remove the <xen:foreach> tags.
i need to look for all users who is user with id 99, how can do this?
For exampla: {$user.user_id.99.username}
 

Robust

Well-known member
What template are you trying to do this in? Sorry, I'm mistaken on what you were trying to do.

If you have an array of users, you can loop over it to get $user and then do your conditional check.

Code:
<xen:foreach loop="$users" value="$user">
<xen:if is="{$user.user_id} == '99'">
 {$user.username} <--- username of user_id 99
</xen:if>
</xenforeach>
I don't know what you have available to you, though. If there is an array of users, I guess it is called $users, but it might not be. You didn't really specify what template you are trying to modify.
 

Chris D

XenForo developer
Staff member
Looks like Robust is asking you the right questions, so if you want help you're going to need to answer them first.
 

netkingZ

Active member
I tried:
Code:
<xen:foreach loop="$users" value="$user">
</xen:foreach>
to get users information , why doesn't works?
Why is a "custom add-on template"?
This code needs to look for users, right?
 

Chris D

XenForo developer
Staff member
i used a specific add-on template

I thinks to get an array of users from loop about $user. $user connect with xf_user , right?

@Chris D and @Robust
I'm not 100% sure, but are you under the impression that you can query the database for specific users in the template? If so, then that's wrong.

The only variables passed to templates are those that are explicitly passed in.
 

Robust

Well-known member
It seems to be like you're trying to modify an add-on, as well. It may be more helpful to ask the resource author for support in the resource thread. If you are modifying a template associated with a new application from an add-on, we can't predict what variables you have available to you and tell you exactly how to do something. The resource author will be the best person to answer these questions.

If this is your own add-on, what template are you editing. If it is included in an existing layout (such as thread_view) then you should state that.

Most importantly, what are you trying to achieve and where are you trying to achieve this? I still don't get what you're trying to achieve. You're trying to print the username of a user if the user ID in the users passed to you is 99, yes? But evidently there are no users passed to that template.

Answering these questions will help us help you, or else we really don't know how to help you.
 

netkingZ

Active member
You're trying to print the username of a user if the user ID in the users passed to you is 99, yes?
Yes , i want this.
But i thinks to understand now.
I seen this response:
The only variables passed to templates are those that are explicitly passed in.
So i have only variable about user_id , but i haven't "access" to forum user db, right?
So i understand that each template is in itself , unless i don't insert into template a specific information, right?

to understand: $user doesn't is a "global variable", right?
otherwise how can access to $user??
 

Robust

Well-known member
Yes , i want this.
But i thinks to understand now.
I seen this response:

So i have only variable about user_id , but i haven't "access" to forum user db, right?
So i understand that each template is in itself , unless i don't insert into template a specific information, right?
That's correct. You don't have access to the database from the template. You only have access to the variables that have been passed in by the controller, and global variables (such as $visitor).
 

netkingZ

Active member
That's correct. You don't have access to the database from the template. You only have access to the variables that have been passed in by the controller, and global variables (such as $visitor).
Ok , now i understand.
So i have only a solution: to edit controller of add-on scripts.
 

Robust

Well-known member
From a controller:

Code:
$user = $this->getModelFromCache('XenForo_Model_User')->getUserById($userId);

$username = $user['username'];
Make sure that $user is populated first, or you'll get an error if it isn't.
 
Top