【问题标题】:PHP Optgroup in ForeachForeach 中的 PHP Optgroup
【发布时间】:2018-08-21 16:22:42
【问题描述】:

我正在制作支付系统下拉系统,但遇到了移动支付 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++;

        }

    }

}

My dropdown

所以我的问题是在每个地区(大陆)的每个移动支付上都进行适当的分组,而不是重复它

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

谢谢。

【问题讨论】:

    标签: php foreach payment optgroup


    【解决方案1】:

    如果您不需要选项中间的 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 查询中加入,同时获取付款方式名称。

    【讨论】:

    • 嗨,非常感谢你,是的,我没想过再多一个 foreach :)) 它的工作原理很完美!你有更强的逻辑思维;)好答案,解释!
    【解决方案2】:

    您的代码中的问题是您使用 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>';
    

    【讨论】:

    • “试试这个”并不是一个好的答案。你应该解释如何为什么这可以解决他们的问题。我推荐阅读,“How do I write a good answer?"
    • 这个不行,所有的移动支付都用手机支付! :/ 谢谢。
    猜你喜欢
    • 1970-01-01
    • 2017-08-17
    • 2020-07-02
    • 1970-01-01
    • 2012-01-27
    • 1970-01-01
    • 2013-05-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多