AFSoar01 AFSoar01 - 4 days ago 4
PHP Question

Incrementing into an Array in PHP function

I'm trying to modify a wordpress plugin and I'm hitting a brick wall with an array that holds variables for a dropdown form field. The final goal is for the user to be able to select a range of times from a dropdown form field.

Here is how the plugin developer suggests using the function:

add_filter('frm_setup_new_fields_vars', 'frm_set_checked', 20, 2);
function frm_set_checked($values, $field){
if($field->id == 125){//Replace 125 with the ID of your field

$values['options'] = array('Option 1', 'Option 2'); //Replace Option1 and Option2 with the options you want in the field
}
return $values;
}


The range of times on the dropdown will not be fixed, so I can't simply type in 'option 1', and 'option 2' - the times will change for each user. So I'm setting the initial time and adding the additional times into the array with a For loop, but it's not working. Here's my attempt - I know the problem lies with the $values['options'] but I'm not sure how to solve it:

add_filter('frm_setup_new_fields_vars', 'frm_set_checked', 20, 2);
function frm_set_checked($values, $field){
if($field->id == 150){//Replace number with the ID of your field
$time = (strtotime("yesterday 20:00"));
$date = date("H:i A", $time);

for ($i = 1; $i <= 96; $i++){
$values['options'] = date("H:i A", $time + 900*$i); //adding 15 additional minutes to each time
}
}
return $values;
}


I also tried this, with no luck:

add_filter('frm_setup_new_fields_vars', 'frm_set_checked', 20, 2);
function frm_set_checked($values, $field){
if($field->id == 150){//Replace number with the ID of your field
$time = (strtotime("yesterday 20:00"));
$date = date("H:i A", $time);

$values = array();
for ($i = 1; $i <= 96; $i++){
$values[$i-1] = date("H:i A", $time + 900*$i);
}
}
return $values;
}


Any help is appreciated!

Answer

Looks to me like you need to return an array with a key called options that contains an array of values. Your 2nd try is almost there. Try collecting all the dates in an array, and then putting them into the $values array. Something like:

    //$values = array();
    $dates = array();
    for ($i = 1; $i <= 96; $i++){
      $dates[] = date("H:i A", $time + 900*$i); 
    }
    $values['options'] = $dates;
    return $values;

(Updated to comment out setting $values variable.)

Comments