Marcel Epp Posted September 1, 2017 Posted September 1, 2017 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); } }
dragan Posted September 1, 2017 Posted September 1, 2017 You could check existing users with the role anzeigen-anbieter, get the highest customer_number, and add 1 for your new customer. $pages->find('template=user,sort=-id,limit=1,roles=anzeigen-anbieter'); 1
Marcel Epp Posted September 2, 2017 Author Posted September 2, 2017 Hello dragan, thanks for your answer. I don't get it to work. 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');
Marcel Epp Posted September 2, 2017 Author Posted September 2, 2017 Or can i query the database direct? to get the latest number from the field?
rick Posted September 2, 2017 Posted September 2, 2017 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.
Marcel Epp Posted September 3, 2017 Author Posted September 3, 2017 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.
dragan Posted September 3, 2017 Posted September 3, 2017 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. 3
maxf5 Posted September 3, 2017 Posted September 3, 2017 $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(); 3
Marcel Epp Posted September 3, 2017 Author Posted September 3, 2017 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); 1
LostKobrakai Posted September 4, 2017 Posted September 4, 2017 Just a word of caution. Do not use this in high traffic pages, because it holds a race condition, where people might be assigned the same number. 2
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now