Ok, I've made progress and basically some of the problems are a combination of me being dumb and Gravity Forms.
I've managed step 3 and formed a full user id AB12CD-001.
Now as for checking it, Wordpress has a function for this pre-built so I'm intending to use that but I just can't seem to form the loop properly.
The function is username_exists($username) - see here: http://codex.wordpress.org/Function_...sername_exists.
I want a loop to use this to check but it returns either the username or NULL and all I can only seem to think of it in terms of IF statements which I know will handle the check but not the increment.
I think I need a do, while loop but don't know how to structure it, here's the latest code:
Code:
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);
$user_count_id_no = 001;
$field['defaultValue'] = build_id($user_id, $user_count_id_no);
}
return $form;
}
function build_id ($pcode_id, $assigned_id_no) {
$cmplt_id = $pcode_id . "-" . number_pad($assigned_id_no, 3);
return $cmplt_id;
}
function number_pad($number,$n) {
return str_pad((int) $number,$n,"0",STR_PAD_LEFT);
}
Bookmarks