PHP范围& foreach,使用当前值? (WordPress的)

时间:2015-02-27 14:18:08

标签: php database wordpress loops foreach

我正在编写一个wordpress网站。 我一直试图解决这个问题,但我无法弄清楚出了什么问题!

所以这是我的问题的简化版本:

HTML

我有一个包含选择字段的列表,每个列表项都有一个类:

<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>

数据库

在我的WP选项数据库中,我保存了几个选项值,我想将其自动添加到右侧选择字段中。以下是保存选项的方法:

option_name:option_value

对于select 1(class-1):

select_1_option_1:选项1(1)

select_1_option_2:选项2(1)

select_1_option_3:选项3(1)

对于select 2(class-2):

select_2_option_1:选项1(2)

select_2_option_2:选项2(2)

select_2_option_3:选项3(2)

等...

PHP

所以基本上,现在我要做的是检查选择字段的类名,并查询选项以填充选择字段。这是我做的:

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:忘了钩子,我正在使用Gravity Form,所以我在表单渲染之前挂钩了我的函数。

基本上

从这里开始的一切都很顺利,但现在当我查看我的选择字段时,第一个是正常的,但第二个和第三个也使用之前的选择字段选项,我不明白?

<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>

你能帮助我这些人,我希望我的选择领域只使用他们的选项,但我不知道是什么让这件事发生了?

非常感谢!

2 个答案:

答案 0 :(得分:1)

我不确定,但试试这个:

  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.


           }
        }

答案 1 :(得分:0)

首先获取一个看起来像这样的数组(使用mysql_query()或PDO等):

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)"
}

然后,

<?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:欢迎使用StackOverflow,以及格式正确的问题!