I’m currently coding a wordpress site.
I’ve been trying to solve this but i can’t figure out what’s going wrong !
So here is a simplified version of my problem :
HTML
I have a list with select fields, each list item has a class :
<ul>
<li class="class-1">
<select>
<option value=""></option>
</select>
</li>
<li class="class-2">
<select>
<option value=""></option>
</select>
</li>
<li class="class-3">
<select>
<option value=""></option>
</select>
</li>
</ul>
Database
In my WP options database, i have several option values saved which I want to be automatically added to the right select field. Here is how the options are saved :
option_name : option_value
For select 1 (class-1) :
select_1_option_1 : Option 1 (1)
select_1_option_2 : Option 2 (1)
select_1_option_3 : Option 3 (1)
For select 2 (class-2) :
select_2_option_1 : Option 1 (2)
select_2_option_2 : Option 2 (2)
select_2_option_3 : Option 3 (2)
etc…
PHP
So basically, now what i’m trying to do is to check the select field’s class name, and query the options to fill the select field. Here is what i did :
function dynamic_options($form) {
foreach($form['fields'] as &$field){
//I call every single field on the form.
$values = range(1, 10);
// I create a range (1 to 10 so we can add up to 10 select fields) to use it in the loop.
foreach($values as $v){
// For each range value as $v : 1 then 2 then 3... to 10.
$class = 'class-' . $v;
if(strpos($field['cssClass'], $class) === false)
continue;
//Check if this field has class name 'class-' . $v. Using $v current value, so basically first call : class-1, second call : class-2...
//If false, continue to the next field, if true :
$rowname = 'select_' . $v . '_option_%';
$prerowname = 'select_' . $v . '_option_';
//Establish vars using the current $v value, example $rowname first call : select_1_option_%, second call : select_2_option_%.
global $wpdb;
$rows = $wpdb->get_results($wpdb->prepare( " SELECT * FROM $wpdb->options WHERE option_name LIKE '$rowname' " ));
//Find row in database where option_name is like our var, first call : select_1_option_%, etc...
if( $rows ){
foreach( $rows as $row ) {
preg_match('([0-9]+)', substr($row->option_name,10), $matches);
//For each row, find the second number (I offset the first 10 positions, so we skip the first number and care only about the last one).
$name = $prerowname . $matches[0];
//Create the full option name using our previous established var and the match.
$optionname = get_option($name);
//Get the option value.
$choices[] = array('text' => $optionname);
$field['choices'] = $choices;
//Return the option value as a new select field option.
}
}
}
}
return $form;
}
add_filter("gform_pre_render_1", "dynamic_options");
PS : Forget about the hook, i’m using Gravity Form so i hook my function before my form renders.
So basically
Everything from here is going so well, but now when i look at my select fields, the first on is ok, but the second and third one use the previous select fields options as well, and i don’t get it ?
<ul>
<li class="class-1">
<select>
<option value="Option 1 (1)"></option>
<option value="Option 2 (1)"></option>
<option value="Option 3 (1)"></option>
</select>
</li>
<li class="class-2">
<select>
<option value="Option 1 (1)"></option>
<option value="Option 2 (1)"></option>
<option value="Option 3 (1)"></option>
<option value="Option 1 (2)"></option>
<option value="Option 2 (2)"></option>
<option value="Option 3 (2)"></option>
</select>
</li>
<li class="class-3">
<select>
<option value="Option 1 (1)"></option>
<option value="Option 2 (1)"></option>
<option value="Option 3 (1)"></option>
<option value="Option 1 (2)"></option>
<option value="Option 2 (2)"></option>
<option value="Option 3 (2)"></option>
<option value="Option 1 (3)"></option>
<option value="Option 2 (3)"></option>
<option value="Option 3 (3)"></option>
</select>
</li>
</ul>
Can you help me guys with this, i would love my select fields to use only THEIR options, but i can’t get what’s making this happening ?
Thanks a lot !
I am not sure, but just try this:
Get an array first that looks like this (using
mysql_query()
or PDO etc):Then,
PS: Welcome to StackOverflow, and nicely formatted question!