Advertisement
chrishajer

Read CSV file and validate code against one column

Sep 12th, 2012
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.71 KB | None | 0 0
  1. // http://www.gravityhelp.com/forums/topic/querying-external-dataset-to-validate-number-input
  2. // Chris Hajer September 12, 2012
  3. // dataset looks like this:
  4. /*
  5.  
  6.     SBCID;Church;Assn;Messengers
  7.     12345;Foo;Bar;2
  8.     67890;Bar;Foo;10
  9.  
  10. */
  11. // validate SBC ID
  12. add_filter('gform_validation_3', 'sbc_validate_code');
  13. function sbc_validate_code($validation_result){
  14.  
  15.         // This calls the sbcid.csv file and sends it to the processor function above
  16.         $csvdata = sbc_csv_in_array( "/home/filelocation/sbcid.csv", ";", "\"", true );
  17.  
  18.         if(!sbc_is_code_valid($csvdata, $_POST['input_1'])){
  19.                 $validation_result['is_valid'] = false;
  20.                 foreach($validation_result['form']['fields'] as &$field){
  21.                 // field 1 is the field where we want to show the validation message
  22.                         if($field['id'] == 1){
  23.                                 $field['failed_validation'] = true;
  24.                                 $field['validation_message'] = 'The SBC ID you entered is invalid: please try again.';
  25.                                 break;
  26.                         }
  27.                 }
  28.         }
  29.         return $validation_result;
  30. }
  31.  
  32. // This creates the array from the data file in the function below
  33. function sbc_csv_in_array($url,$delm=";",$encl="\"",$head=false) {
  34.  
  35.         $csvxrow = file($url);   // ---- csv rows to array ----
  36.  
  37.         $csvxrow[0] = chop($csvxrow[0]);
  38.         $csvxrow[0] = str_replace($encl,'',$csvxrow[0]);
  39.         $keydata = explode($delm,$csvxrow[0]);
  40.         $keynumb = count($keydata);
  41.  
  42.         if ($head === true) {
  43.         $anzdata = count($csvxrow);
  44.         $z=0;
  45.         for($x=1; $x<$anzdata; $x++) {
  46.                 $csvxrow[$x] = chop($csvxrow[$x]);
  47.                 $csvxrow[$x] = str_replace($encl,'',$csvxrow[$x]);
  48.                 $csv_data[$x] = explode($delm,$csvxrow[$x]);
  49.                 $i=0;
  50.                 foreach($keydata as $key) {
  51.                         $out[$z][$key] = $csv_data[$x][$i];
  52.                         $i++;
  53.                         }
  54.                 $z++;
  55.                 }
  56.         }
  57.         else {
  58.                 $i=0;
  59.                 foreach($csvxrow as $item) {
  60.                         $item = chop($item);
  61.                         $item = str_replace($encl,'',$item);
  62.                         $csv_data = explode($delm,$item);
  63.                         for ($y=0; $y<$keynumb; $y++) {
  64.                            $out[$i][$y] = $csv_data[$y];
  65.                         }
  66.                 $i++;
  67.                 }
  68.         }
  69. return $out;
  70. }
  71.  
  72. // This searches the array to see if the code is valid and returns TRUE or FALSE
  73. function sbc_is_code_valid($csvdata, $thiscode) {
  74.         foreach ($csvdata as $key => $value) {
  75.                 if ($thiscode == $value['SBCID'])
  76.                         return TRUE;
  77.                 else
  78.                         return FALSE;
  79.         }
  80. }
  81.  
  82. // doing this here because the redirect URL does not support variables or shortcodes
  83. add_filter('gform_confirmation_3', 'valid_sbcid_confirmation', 10, 4);
  84. function valid_sbcid_confirmation($confirmation, $form, $lead, $ajax){
  85.         // customize this URL - I send the code in the query string
  86.         $success_url = 'http://example.com/next-form/?sbcid=' . $lead[1];
  87.         $confirmation = array('redirect' => $success_url);
  88.         return $confirmation;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement