[英]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.