Jump to content

[Solved]Username = Costumer Number


Marcel Epp
 Share

Recommended Posts

Hi,

i'm looking for a solution to register users in the frontend. For the username i would use a number with 6 digits starting with 300000.

I have set up a field called customer_number. It is possible to give the field a number and then count up.

300001 user1

300002 user2

At the moment my code looks like this:

if($input->post->create_user_submit) {

    //instantiate variables taking in the form data
    $email = $sanitizer->email($input->post->user_email);

    if (in_array($input->post->email, $email)) {
    	echo "<h5 class='error'>E-Mail is already in use.</h5>";
    } else {
    //create user details
    	$user = new User();
      $user->of(false);
      $user->name = "Paul";
      $user->pass = "max123";
      $user->email = $email;
      $user->addRole("anzeigen-anbieter");
      $user->save();
      $user->of(true);
    }
}

 

Link to comment
Share on other sites

Hello dragan,

thanks for your answer. I don't get it to work. :undecided:

If i use the code from above i get zero a 0. I tried to fetch the highest customer_number with $user. But this didn't work either.

items = $users->find('sort=-id,limit=1,roles=anzeigen-anbieter');

 

Link to comment
Share on other sites

On 9/1/2017 at 6:55 AM, Marcel Epp said:

Hi,

i'm looking for a solution to register users in the frontend. For the username i would use a number with 6 digits starting with 300000.

I have set up a field called customer_number. It is possible to give the field a number and then count up.

300001 user1

300002 user2

At the moment my code looks like this:


if($input->post->create_user_submit) {

    //instantiate variables taking in the form data
    $email = $sanitizer->email($input->post->user_email);

    if (in_array($input->post->email, $email)) {
    	echo "<h5 class='error'>E-Mail is already in use.</h5>";
    } else {
    //create user details
    	$user = new User();
      $user->of(false);
      $user->name = "Paul";
      $user->pass = "max123";
      $user->email = $email;
      $user->addRole("anzeigen-anbieter");
      $user->save();
      $user->of(true);
    }
}

 

Where are you saving the customer_number field value? If it is not set then you will get a default return value 0 from your selector depending on the field setting you have in place.

Link to comment
Share on other sites

Hi Rick,

at the moment i didn't save the number. My first thought was to get the field (customer_number) and the latest number and echo that out.

I would see if this would work. But for me it's not really clear how to get the number. I tried dragans way and my own. 

Link to comment
Share on other sites

I just tried this and it works as expected:

$highestID = $pages->get("template=user, sort=-id, limit=1, roles=intern")->id;
echo $highestID;

in your case, you'd have to replace id with customer_number, and use your specific role anzeigen-anbieter.

  • Like 3
Link to comment
Share on other sites

$latest = $users->find('roles=anzeigen-anbieter, sort=-customer_number, limit=1');
$newnumber = $latest->customer_number + 1;

$user = new User();
$user->of(false);
$user->name = "Paul";
$user->pass = "max123";
$user->email = $email;

$user->customer_number = $newnumber;

$user->addRole("anzeigen-anbieter");
$user->save();

 

  • Like 3
Link to comment
Share on other sites

Thanks dragan! and thanks maxf5 you really helped me out! It is working now. I think i mixed your last entries.

The code i use now:

// find the highest customer number
$highest_customer_number = $pages->get("template=user, sort=-id, limit=1, roles=anzeigen-anbieter")->customer_number;
// add +1
$customer_number_plus_one = $highest_customer_number + 1;

//create user details
$user = new User();
$user->of(false);
$user->pass = rand_string(8);
$user->email = $email;
$user->customer_number = $customer_number_plus_one;
$user->addRole("anzeigen-anbieter");
$user->save();
$user->of(true);

 

  • Like 1
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...