Jump to content

Recommended Posts

Posted

As a check for my form, I'm looking to do an 'in_array' check for the username input but I can't seem to output all usernames into an array.

I'm sure I'm missing something really stupid but can't seem to get the desired result!

  • Like 1
Posted

This seems to return all my users:

$users->find("start=0")

Thanks Arjen. But I seem to be getting "Error call on a non-object" whenever I try to access $users?

  • Like 1
Posted

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

Posted

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.

Posted
foreach ($users->find("start=0") as $u)
{
    echo $u->name;
}

Doesn't output anything? There should be at least to users: guest and you as admin.

  • Like 1
Posted
foreach ($users->find("start=0") as $u)
{
    echo $u->name;
}

Doesn't output anything? There should be at least to users: guest and you as admin.

Yes thanks, that outputs the names but I can't seem to be able to store them in an array?

Posted

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
Posted
$userNames = array();
foreach ($users->find("start=0") as $u)
{
    array_push($userNames, $u->name);
}

This should place all the usernames in an array.

  • Like 1
Posted

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
Posted

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.

Posted

Than you should use soma's solution

if(!$users->get($username)){
   // go ahead, add the guy
}

edit: but it's good that you learned the array stuff :)

  • Like 3

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
×
×
  • Create New...