I have the following (partly) code which will check the values entered in a form with the arrays (like a coupon code check). If entered value in a form is one of those in the array people can enter the code in the form.
if(!in_array($posted_value, array('DA001','DA002'))){ //
So I have a csv file with 80.000 codes. Is there anyway (online converter or something) I can put all the codes in between ' ' and at a , so csv right now is:
DA001
DA002
DA003
IE302
I want to convert it to 'DA001´, 'DA002', 'DA003', 'IE302'
--- This is my full code, with your code included: I have the codes.csv in the same directory as the .php file This is my code now, but something goes wrong since I have 500 server error.
add_filter('frm_validate_field_entry', 'my_custom_validation', 10, 3);
function my_custom_validation($errors, $posted_field, $posted_value){
if($posted_field->id == 9){ //change 25 to the ID of the field to validate
$codes = file("codes.csv", FILE_IGNORE_NEW_LINES);
if (!in_array($posted_value, static $codes = array_flip(...);))){ //change 001 and 002 to your allowed values
//if it doesn't match up, add an error:
$errors['field'. $posted_field->id] = 'Deze code is al een keer gebruikt of bestaat niet.';
}
}
return $errors;
}
Use the file()
function to read a file into an array. Each line will become an array element.
$codes = file("codes.csv", FILE_IGNORE_NEW_LINES);
if (!in_array($posted_value, $codes)) {
...
}
However, searching an array with 80K elements will be slow. If you're doing this repeatedly in the same script, it would be a good idea to hash them, by converting it to an associative array:
$codes = array_flip(file("codes.csv", FILE_IGNORE_NEW_LINES));
if (!isset($codes[$posted_value])) {
...
}
The full code should be:
add_filter('frm_validate_field_entry', 'my_custom_validation', 10, 3);
function my_custom_validation($errors, $posted_field, $posted_value){
if($posted_field->id == 9){ //change 25 to the ID of the field to validate
static $codes;
if (!$codes) {
$codes = array_flip(file("codes.csv", FILE_IGNORE_NEW_LINES));
}
if (!isset($codes[$posted_value])){ //change 001 and 002 to your allowed values
//if it doesn't match up, add an error:
$errors['field'. $posted_field->id] = 'Deze code is al een keer gebruikt of bestaat niet.';
}
}
return $errors;
}