Advertisement
chrishajer

Read CSV and compare code then populate fields (wedding)

Mar 1st, 2013
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.92 KB | None | 0 0
  1. // http://www.gravityhelp.com/forums/topic/wedding-rsvp-dynamic-population
  2. // originally http://www.gravityhelp.com/forums/topic/database-look-up-validation
  3. // validate a code entered in field 1 of your form with a text file
  4. // change the 2 here to your form ID
  5. add_filter('gform_validation_2', 'validate_code');
  6. function validate_code($validation_result){
  7.         // this assumes the code is entered in field one on your form
  8.         // change this input_ number if it's a different field
  9.         // is_code_valid function can be found below.  This calls the function and if FALSE is returned, a validation error is returned
  10.         if(!is_code_valid($_POST['input_1'])){
  11.         $validation_result['is_valid'] = false;
  12.         foreach($validation_result['form']['fields'] as &$field){
  13.             // field 1 is the field where we want to show the validation message
  14.             if($field['id'] == 1){
  15.                 $field['failed_validation'] = true;
  16.                                 $field['validation_message'] = 'The code you entered is invalid: please try again.';
  17.                 break;
  18.             }
  19.         }
  20.     }
  21.     return $validation_result;
  22. }
  23.  
  24. // use this function to validate the entered code
  25. // called from the validate_code function above
  26. function is_code_valid($thiscode){
  27.         // read all the codes in from the guests.txt file
  28.         // change the name and location of this file
  29.  
  30.         /* here is the format of the file
  31.         12345,Chris Hajer,Abraham Lincoln
  32.         56789,Phillipo Berrio,Ron Paul,George Washington
  33.         222,Don King,Jesse Ventura,Jeb Bush,Barack Obama
  34.         5566,The Situation
  35.         */
  36.  
  37.         $codes = file('/home/content/html/website/guests.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  38.         // read each line and split the line into the $codes array, on commas
  39.         foreach($codes as $code){
  40.                 $guests = explode (",", $code);
  41.                 // compare the entered code to all codes in the file until we find a match
  42.                 // the [0] indexed item is our code
  43.                 if($thiscode == $guests[0]){
  44.                         // the entered code matched, so grab all the names AND return true
  45.                         // the populate_guest_names function can be found below
  46.                         populate_guest_names($guests);
  47.                         // return true because this is a valid code
  48.                         return TRUE;
  49.                 }
  50.         }
  51.         // if we did not have a match and are out of codes, return FALSE
  52.         return FALSE;
  53. }
  54.  
  55. // populate all the guest names that are associated with this password
  56. // this function accepts an array which is the code and all the names associated with it
  57. // called from the is_code_valid function above
  58. function populate_guest_names($names){
  59.  
  60.         // count the number of guests associated with this code
  61.         // subtract 1 so we don't count the password as an array item
  62.         $number_of_names = count($names)-1;
  63.  
  64.         // the functions below can be simplified and don't account for any number of guests less than 1 or more than 4
  65.         if ($number_of_names == 1) {
  66.                 // split the $names[1] array item on the space between first and last name
  67.                 $guest1 = explode (" ",$names[1]);
  68.                 // assign the first and last name to the $_POST to populate hidden inputs in the form
  69.                 $_POST['input_2'] = $guest1[0];
  70.                 $_POST['input_3'] = $guest1[1];
  71.  
  72.         } elseif ($number_of_names == 2) {
  73.                 // repeat the same process for 2, 3 or 4 names, just assigning more variables
  74.                 $guest1 = explode (" ",$names[1]);
  75.                 $_POST['input_2'] = $guest1[0];
  76.                 $_POST['input_3'] = $guest1[1];
  77.  
  78.                 $guest2 = explode (" ",$names[2]);
  79.                 $_POST['input_4'] = $guest2[0];
  80.                 $_POST['input_5'] = $guest2[1];
  81.  
  82.         } elseif ($number_of_names == 3) {
  83.  
  84.                 $guest1 = explode (" ",$names[1]);
  85.                 $_POST['input_2'] = $guest1[0];
  86.                 $_POST['input_3'] = $guest1[1];
  87.  
  88.                 $guest2 = explode (" ",$names[2]);
  89.                 $_POST['input_4'] = $guest2[0];
  90.                 $_POST['input_5'] = $guest2[1];
  91.  
  92.                 $guest3 = explode (" ",$names[3]);
  93.                 $_POST['input_6'] = $guest3[0];
  94.                 $_POST['input_7'] = $guest3[1];
  95.  
  96.         } else {
  97.  
  98.         $guest1 = explode (" ",$names[1]);
  99.         $_POST['input_2'] = $guest1[0];
  100.         $_POST['input_3'] = $guest1[1];
  101.  
  102.         $guest2 = explode (" ",$names[2]);
  103.         $_POST['input_4'] = $guest2[0];
  104.         $_POST['input_5'] = $guest2[1];
  105.  
  106.         $guest3 = explode (" ",$names[3]);
  107.         $_POST['input_6'] = $guest3[0];
  108.         $_POST['input_7'] = $guest3[1];
  109.  
  110.         $guest4 = explode (" ",$names[4]);
  111.         $_POST['input_8'] = $guest4[0];
  112.         $_POST['input_9'] = $guest4[1];
  113.  
  114.         }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement