Hi everyone,

I'm new here but have been trying to find some help with some PHP.

This topic kinda spans PHP and Wordpress but the actual question is pure PHP so I hope I've put this in the right place.

Basically I'm developing a Wordpress website that uses the Gravity Forms User Registration plugin to handle sign ups, and due to the nature of the site I want the user names to be predefined by the site using the postcode the user entered on the first page of the form. The logic is as follows:

Get postcode from form page 1
Make sure postcode is capitalized and remove spaces
Add -001 number to the end of the postcode
Check if this complete username has been used
If it has increase to 002 and try again
If it has not been used continue
Send authorized username back to the form

Final format = AB12CD-001

I can get the postcode and format it correctly and send it back to the form fine so interfacing with GForms isn't the issue here, but it's the adding number section that I'm struggling with. Here's what I've done so far:

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'm really struggling, I know it has to loop but I can't seem to get it right, any help will be greatly received guys!