简体   繁体   中英

PHP Optgroup in Foreach

i'm on making payment system dropdown system, and have issue, with optgroup for mobile payments. This is example, of response from 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"
}

So where ['type'] === 20 && ['mobile'] != 0 i need for making optgroup with label mobile payments..

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

So my problem is to make proper group without repeating it on every mobile payments in each region (continent)

Should be soemthing like this: https://jsfiddle.net/atc67mLe/24/

Thank you.

If you don't need the optgroup in the middle of your options, you could just add it at the end.

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>';
    }
}

This is untested, but you get the idea. Just separate the mobile ones into a new array and loop through them to make the optgroup afterwards.

I would probably try to refine the initial SQL query though, to retrieve a more usable set of results rather than running a bunch of extra queries in the loop for every option. It would be better to have a join in your first SQL query, to get the payment method name at the same time.

the issue in your code is you wrapping each mobile payment option with optgroup. What you have to do is to group all mobile payment options in array or in a string and then wrapping them all with optgroup before print them out

here is an example ...

$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>';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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