繁体   English   中英

Foreach中的PHP Optgroup

[英]PHP Optgroup in Foreach

我正在制作付款系统下拉系统,并且在optgroup进行移动支付时遇到了问题。 这是来自MySQL的响应示例。

[17]=>
array(5) {
["id"]=>
string(2) "34"
["region"]=>
string(1) "1"
["type"]=>
string(2) "18"
["mobile"]=>
string(1) "0"
["system"]=>
string(1) "1"
}
[18]=>
 array(5) {
["id"]=>
string(2) "35"
["region"]=>
string(1) "1"
["type"]=>
string(2) "19"
["mobile"]=>
string(1) "0"
["system"]=>
string(1) "1"
 }
[19]=>
array(5) {
["id"]=>
string(2) "36"
["region"]=>
string(1) "1"
["type"]=>
string(2) "20"
["mobile"]=>
string(1) "1"
["system"]=>
string(1) "1"
}
[20]=>
array(5) {
["id"]=>
string(2) "37"
["region"]=>
string(1) "1"
["type"]=>
string(2) "20"
["mobile"]=>
string(1) "2"
["system"]=>
string(1) "1"
}
[21]=>
array(5) {
["id"]=>
string(2) "38"
["region"]=>
string(1) "1"
["type"]=>
string(2) "20"
["mobile"]=>
string(1) "3"
["system"]=>
string(1) "1"
}

因此,在['type'] === 20 && ['mobile']!= 0的情况下,我需要使用标签移动支付来创建optgroup。

if ( ! empty ( $regions ) && $regions !== NULL ) {

    $array = array();

    $im = 0;
    $i = 0;

    var_dump( $regions );

    foreach ( $regions as $key => $region ) {

        if ( (int) $region['mobile'] === 0 ) {
            $type  = $db->getTypeById( (int) $region['type'] );
            $value = $region['type'];

            echo "<option value='{$value}'>{$type}</option>";

        } else {

            //if ( $i < 1 )
            //echo '<optgroup label="Mobile payments">';

            $type  = $db->getMobileById( (int) $region['mobile'] );
            $value = $region['type'] . '_' . $region['mobile'];

            echo "<option value='{$value}'>{$type}</option>";

            //if ( $i <= $im )
            //echo '</optgroup>';

            $i++;

        }

    }

}

我的下拉菜单

因此,我的问题是在每个区域(大陆)的每次移动支付中都没有重复设置适当的分组

应该是这样的东西: https ://jsfiddle.net/atc67mLe/24/

谢谢。

如果您不需要选项中间的optgroup,则可以在最后添加它。

if (!empty($regions)) {

    $mobileOptions = [];

    // Print normal options
    foreach ($regions as $region) {

        $typeId = $region['type'];

        if ($typeId == 20 && $region['mobile'] != 0) {
            $mobileOptions[] = $region;
        } else {
            $label = $db->getTypeById($typeId);
            echo "<option value='{$typeId}'>{$label}</option>";
        }
    }

    // Print mobile options
    if (!empty($mobileOptions)) {
        echo '<optgroup label="Mobile payments">';

        foreach ($mobileOptions as $region) {

            $mobileId = $region['mobile'];
            $typeId = $region['type'];
            $label  = $db->getMobileById($mobileId);

            echo "<option value=\"{$typeId}_{$mobileId}\">{$label}</option>";
        }
        echo '</optgroup>';
    }
}

这未经测试,但是您知道了。 只需将移动设备分成一个新阵列,然后遍历它们,然后再制成optgroup。

不过,我可能会尝试优化初始SQL查询,以检索更有用的结果集,而不是在每个选项的循环中运行一堆额外的查询。 最好在第一个SQL查询中加入一个联接,以同时获取付款方式名称。

代码中的问题是您用optgroup封装了每个移动支付选项。 您要做的是将所有移动支付选项以数组或字符串分组,然后在将它们全部打印出来之前用optgroup包裹它们

这是一个例子...

$options = '';
$mobileOptions = '';

foreach ($regions as $region) {

    $typeId = $region['type'];

    if ($typeId == 20 && $region['mobile'] != 0) {

        $label  = $db->getMobileById($region['mobile']);
        $mobileOptions .= sprintf('<option value="%s_%s">%s</option>',$typeId,$region['mobile'],$label);

    } else {

        $label = $db->getTypeById($typeId);
        $options .= sprintf('<option value="%s">%s</option>',$typeId,$label);

    }

}

echo $options;
if ($mobileOptions)
    echo '<optgroup label="Mobile payments">'.$mobileOptions.'</optgroup>';

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM