PHP range & foreach, use current value? (WordPress)

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 :

Read More

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 !

Related posts

Leave a Reply

2 comments

  1. I am not sure, but just try this:

      if( $rows ){
            $choices=array();
                    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.
    
    
               }
            }
    
  2. Get an array first that looks like this (using mysql_query() or PDO etc):

    array(4) {
      ["select_1_option_1"]=>
      string(12) "Option 1 (1)"
      ["select_1_option_2"]=>
      string(12) "Option 2 (1)"
      ["select_2_option_1"]=>
      string(12) "Option 1 (2)"
      ["select_2_option_2"]=>
      string(12) "Option 2 (2)"
    }
    

    Then,

    <?php
    function optionParser(array $parseInput = array(), array $parsedOptions = array()) {
        if (empty($parseInput)) {
        return $parsedOptions;
      }
    
        $optionNames = array_keys($parseInput);
    
        $processedOption = $po = explode('_', $optionNames[0]);
    
        $parsedOptions[$po[1]][$po[3]] = $parseInput[$optionNames[0]];
    
        return optionParser(array_slice($parseInput, 1), $parsedOptions);
    }
    
    // assuming the array I asked for in the answer above is called $arrayFromMySQL
    $arrayForHtml = optionParser($arrayFromMySQL);
    ?>
    <ul>
        <?php foreach($arrayForHtml as $classKey => $optionSet) { ?>
            <li class="class-<?php echo $classKey;?>">
            <select>
                <?php foreach($optionSet as $option) {
                    <option value="<?php echo $option;?>"></option>
                } ?>
            </select>
            </li>
        <?php } ?>
    </ul>
    

    PS: Welcome to StackOverflow, and nicely formatted question!