【问题标题】:PHP grouping of options in optgroupoptgroup 中选项的 PHP 分组
【发布时间】:2020-07-02 06:28:19
【问题描述】:

我正在开发一个 woocommerce 网站,我需要找到一种方法来对 optgroups 中的选择选项进行分组。

我的选项存储为数组,如

aray = [  
            "A4 1.9tdi (2003-2009)",  
            "A4 2.0tdi (2003-2009)",  
            "Passat B7 1.9tdi(2003-2009)",  
            "Passat B7 2.0 tdi(2003-2010)"  
        ]  

我需要的是通过 php 进行选择,通过使用字符串到第一个空格将 optgroup 中的选项分组

通过使用函数

explode(' ',$s, 2) or by strpos($inputString, ' ');

我可以根据需要拆分值。

我需要修改我当前用于显示选项的代码:

$html = '<span class="number">' . ($level + 1).'</span><select class="'.$class.'" name="'.$levelData['url_parameter'].'" '.$extra.'>;
    foreach ( $this->getLevelOptions($level) as $val ){
      $html .= '<option value="'.esc_attr( $val ).'" '.($val == $value ? 'selected' : '').'>'.esc_html( $val ).'</option>';         
    }
    $html .= '</select>';

    return $html;    

所以我可以显示按 optgroup 分组的选项,例如:

A4 (optgroup)
A4 1.9tdi (2003-2009)  
A4 2.0tdi (2003-2009) 
Passat (optgroup)
Passat B7 1.9tdi(2003-2009)  
Passat B7 2.0 tdi(2003-2010)

如果有人可以帮助我,我将不胜感激。

【问题讨论】:

  • 您的 `$html 变量没有正确地用单引号关闭。可能只是复制粘贴错误。
  • 您在寻找&lt;optgroup label="A4"&gt; &lt;option value="1.9tdi(2003-2009)"&gt;1.9tdi(2003-2009)&lt;/option&gt; &lt;option value="2.0td(2003-2009i(2003-2009)"&gt;2.0tdi(2003-2009)&lt;/option&gt; &lt;/optgroup&gt; &lt;optgroup label="Passat"&gt; &lt;option value="1.9tdi(2003-2009)"&gt;1.9tdi(2003-2009)&lt;/option&gt; &lt;option value="2.0tdi(2003-2010)"&gt;2.0tdi(2003-2010)&lt;/option&gt; &lt;/optgroup&gt; 这是您希望的显示方式吗?

标签: php select options optgroup


【解决方案1】:

我不确定我是否理解您的 html 部分。 (至少在手机上很难阅读)
但这可能会做你想要的,只需替换占位符 html 标记。

我首先创建一个与第一个单词(汽车模型)相关联的新数组。
这也可以确保在我开始输出时对其进行排序。

然后我只输出键并内爆子数组中的所有值。

$array = [  
            "A4 1.9tdi (2003-2009)",  
            "A4 2.0tdi (2003-2009)",  
            "Passat B7 1.9tdi(2003-2009)",  
            "Passat B7 2.0 tdi(2003-2010)"  
        ];

foreach($array as $val){
    $temp = explode(" ", $val);
    $new[$temp[0]][] = $val;
}

foreach($new as $car => $cars){
    echo "<some html tag>". $car . "</Some html tag>\n";
    echo "<some other tag>" . implode("</some other tag>\n<some other tag>", $cars) . "</some other tag>\n";
    echo "\n";
}

输出:

<some html tag>A4</Some html tag>
<some other tag>A4 1.9tdi (2003-2009)</some other tag>
<some other tag>A4 2.0tdi (2003-2009)</some other tag>

<some html tag>Passat</Some html tag>
<some other tag>Passat B7 1.9tdi(2003-2009)</some other tag>
<some other tag>Passat B7 2.0 tdi(2003-2010)</some other tag>

https://3v4l.org/N8YTu

【讨论】:

    【解决方案2】:

    这格式化为选项组,将值放在每个选项内的value attr 中。 &lt;option value="1.9tdi (2003-2009)"&gt; 并将其全部包装在一个选择标签中。

    $cars = array(
        "A4 1.9tdi (2003-2009)",  
        "A4 2.0tdi (2003-2009)",  
        "Passat B7 1.9tdi(2003-2009)",  
        "Passat B7 2.0 tdi(2003-2010)"
    );
    
    foreach($cars as $car){
        $model = explode(' ', $car, 2);
        $make[$model[0]][] = $car;  
    }
    
    $stmt = 'Select a car: <select id="cars">';
    foreach($make as $label => $type){
        $stmt .= '<optgroup label="'.$label.'">';
    
        foreach($type as $car){
            list($mk, $cr) = explode(' ', $car, 2);
            $stmt .= '<option value="'.$cr.'">'.$cr.'</option>';
        }
        $stmt .= '</optgroup>';
    }
    
    $stmt .= '</select>';
    

    在 html 中回显您的 $stmt 变量。

    <?=$stmt?>
    

    https://3v4l.org/bb2nR

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-18
      相关资源
      最近更新 更多