Jump to content

Help with Creating Unique User ID's Please!


theslink2000

Recommended Posts

Hi everyone, I'm new here and really in need of some help with some script I've been trying to get right.

 

Basically I'm trying to write a function that will interface with Gravity Forms and Wordpress but this is NOT a GForms or Wordpress issue it's pure PHP honest!

 

The function will pull the post code a user has entered on the first page of a Gravity Form, and then take the spaces out of it and make sure it's in upper case.  This much I've got working.

 

Now I need it to add a hyphen and a three digit number starting at 001, so it'll look like AB12CD-001.  Now it will need to check if this is username has already been used by calling the Wordpress function username_exists (http://codex.wordpress.org/Function_Reference/username_exists) and if it does increase the suffix number by one and recheck until it finds the next available number.

 

This could result in the user living in the post code AB1 2CD ending up with the username AB12CD-021 if there are 20 others in the local area obviously.

 

Now this will then post back to the Gravity Forms registration form on the next page and fill in the username for the user, this I can also do.

 

So hopefully you can see it's the PHP bit I'm stuck on and I would really appreciate any help.

 

Cheers guys  :)

Link to comment
Share on other sites

Is there any reason in particular you're doing it this way? It's not exactly the most efficient way to go about this.

 

I'm guessing every call to username_exists() sends a query to the database. You'd be better off running a single query that gets the largest value that begins with AB12CD, and increasing that by 1.

Link to comment
Share on other sites

Thanks for the quick reply bud.

 

In all honesty there's no particular reason other that it's how it made sense in my head, I'd love any critique of a better way to go about this.  This is as far as I've got:

 

 

add_filter("gform_pre_render_1", "populate_previous_page_data");

function populate_previous_page_data($form){

 

    $page_number = rgpost("gform_target_page_number_{$form["id"]}");  //Set $page_number to the form id ??

    $page_number = !is_numeric($page_number) ? 1 : $page_number; //Make the form id a usable number ??

 

    foreach($form['fields'] as &$field) { //Loop through each form field in the variable $field

 

        if($field['id'] != 9) // If field is not equal to 9 (or target for dynamically populating)

 

        $field_page = rgar($field, 'pageNumber'); //$field_page equals the form page the currently looked at field is on

 

        if($page_number > $field_page) //If $page_number is greater than $field_page then

            continue; //Exit loop and restart

 

        //$field['defaultValue'] = rgpost('input_2_5'); //If conditions are met, ie. the currently field in the loop is the target, set the value to the contents of input_2_5 (or the source required)

 

$user_id = rgpost('input_2_5');

$user_id = preg_replace('/\s+/', '', $user_id);

$user_id = strtoupper($user_id);

 

$count = 1;

$user_id_no = 001;

$field['defaultValue'] = $user_id;

 

    }

 

    return $form;

}

 

 

I know it's a bit messy but I'm still experimenting before tidying it up, needless to say the bottom bit about counting ain't doing much right now.

Link to comment
Share on other sites

Here's another method.

 

Defining an auto_increment field as the  second part of a primary key will add one each time a new one is inserted but will start at 1 for each new first part of the key (postcode in the example)

 

CREATE TABLE IF NOT EXISTS `postcode` (
  `postcode` varchar(7) NOT NULL,
  `id` int(3) unsigned zerofill NOT NULL AUTO_INCREMENT,
  `name` varchar(45) NOT NULL,
  PRIMARY KEY (`postcode`,`id`)
) ENGINE=MyISAM;

INSERT INTO postcode (postcode, name) VALUES
('W1 2AB',  'Fred'),
('L6 3AW', 'Bill'),
('W1 2AB',  'Dave'),
('L6 3AW', 'Mary'),
('L6 3AW', 'Jane');

SELECT * FROM postcode
ORDER BY postcode, id;

Results:
+----------+-----+------+
| postcode | id  | name |
+----------+-----+------+
| L6 3AW   | 001 | Bill |
| L6 3AW   | 002 | Mary |
| L6 3AW   | 003 | Jane |
| W1 2AB   | 001 | Fred |
| W1 2AB   | 002 | Dave |
+----------+-----+------+

 

 

 

 

Link to comment
Share on other sites

Hmmm, I see where you're coming from bud and if I was writing the whole thing from scratch that would be great.  However with gravity forms the data isn't even saved to the database at this point so it wouldn't work unfortunately.

 

As sudo code here's roughly what im thinking:

 

Get postcode

Capitalise and remove spaces from postcode

Add -001 to the end of the postcode to make username

Check if this is already in use

If not proceed

Else increase number by 1 and check again

When usable number is found return username to form

 

In this list its steps 3 - 6 I'm struggling with and I'm sure its just string manipulation and there's no need to for SQL etc.

 

Really appreciating the help guys, anyone else got any further thoughts?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.