Jump to content

How to list all usernames?


onjegolders
 Share

Recommended Posts

Which is the context you are calling this? Template, module? Do you by any change defined manually a variable called $users?

Aha! Yes I did, thanks but do you know how to output the names rather than the whole object? it keeps coming up empty when I try ->title or ->name?

This is in a template.

Link to comment
Share on other sites

This should also output the names

foreach ($users as $u) {
    echo $u->name;
}

And you can put them in an array like this

$names = array();

foreach ($users as $u) {
    $names[] = $u->name;
}

print_r($names);
  • Like 2
Link to comment
Share on other sites

or

if($users->get($username)){
   // username found
}
 

not sure what you're doing exactly as always to give simpler solutions

This would also work,

foreach ($users as $u){
    echo $u->name;
}
 

And if in an function scope where $users is not available:

foreach (wire("users") as $u){
    echo $u->name;
}
  • Like 2
Link to comment
Share on other sites

or

if($users->get($username)){
   // username found
}
 

not sure what you're doing exactly as always to give simpler solutions

This would also work,

foreach ($users as $u){
    echo $u->name;
}
 

And if in an function scope where $users is not available:

foreach (wire("users") as $u){
    echo $u->name;
}

Thanks Soma, 

I'm just using an in_array check for my registration form template to see that their chosen username doesn't already exist.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...