【问题标题】:PHP range & foreach, use current value? (Wordpress)PHP范围和foreach,使用当前值? (WordPress)
【发布时间】:2015-04-30 06:57:42
【问题描述】:

我目前正在编写一个 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

对于选择 1(1 类):

select_1_option_1:选项 1 (1)

select_1_option_2:选项 2 (1)

select_1_option_3:选项 3 (1)

对于选择 2(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:忘记钩子了,我使用的是重力形式,所以我在表单呈现之前钩子了我的函数。

基本上

这里的一切都很顺利,但是现在当我查看我的选择字段时,第一个还可以,但是第二个和第三个也使用以前的选择字段选项,我不明白吗?

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

你们能帮我解决这个问题吗,我希望我的选择字段只使用他们的选项,但我不知道是什么导致了这种情况发生?

非常感谢!

【问题讨论】:

    标签: php database wordpress loops foreach


    【解决方案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.
    
    
               }
            }
    

    【讨论】:

    • 我尝试了 $choices[0],现在每个选择字段只有一个选项,只有最后一个选项,但不再是每个字段中的所有选项,所以这是一个好的开始!其他 $choices 不起作用。
    • 哇!它的工作,你的先生,让我的一天!这么快,这么好,成功了!非常感谢!!!
    【解决方案2】:

    首先获取一个如下所示的数组(使用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,这是一个格式精美的问题!

    【讨论】:

    • 感谢您的建议,我早先得到了我想要的东西。感谢您的支持和欢迎;)
    • 没问题!我主要写了答案,因为我将其视为一个递归问题,并想看看以这种方式解决它需要多长时间;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-04
    相关资源
    最近更新 更多