在多个选择框

时间:2016-09-17 14:01:16

标签: php loops for-loop foreach

我有一个数组如下

$myArray = array('a','b','c','d');

我需要将已经保存在数据库中的一些值分配到不同数量的选择框中,让我们说4个列表框,这样这些保存的值将在第一个列表框中显示为选中剩下的将是默认值,并且我们说保存的值是 a和c

所以基于输出应该有4个列表框以(选定)和第二个列表框c(选定)开头,其他2个列表框应该保持默认类似

<select id="id" name="id" class="form-control">
    <option value="">No selection</option>
    <option value="a" selected>a</option>
    <option value="b">b</option>
    <option value="c">c</option>
    <option value="d">d</option>
</select>

<select id="id" name="id" class="form-control">
    <option value="">No selection</option>
    <option value="a">a</option>
    <option value="b">b</option>
    <option value="c" selected>c</option>
    <option value="d">d</option>
</select>

<select id="id" name="id" class="form-control">
    <option value="" selected>No selection</option>
    <option value="a">a</option>
    <option value="b">b</option>
    <option value="c">c</option>
    <option value="d">d</option>
</select>

<select id="id" name="id" class="form-control">
    <option value="" selected>No selection</option>
    <option value="a">a</option>
    <option value="b">b</option>
    <option value="c">c</option>
    <option value="d">d</option>
</select>

修改 这是我的代码,我循环生成7或9列表框根据传递的值和内部i循环$municipals以显示所有市政和市政存在于DB中保存值的数组我需要在第一个选择框中显示,依此类推

                    <?php if(!empty($municipal_number->meta_value)):
                        $ids = array();

                        foreach($municipal_ids as $municipal_id) {
                            $ids[] = $municipal_id->meta_value;
                        }

                    if($municipal_number->meta_value == 7):  
                        for ($x = 1; $x <= 7; $x++): ?>
                            <div class="col-lg-3">
                                <label>Select Municipal</label>

                                <select id="municipal_id_<?php echo $x; ?>" name="municipal_id[]" class="municipal_id form-control">
                                    <option value="0" selected>No Municipal</option>

                                    <?php foreach($municipals as $municipal): ?>
                                        <?php if(in_array($municipal->id, $ids)): ?>
                                            <option selected value="<?php echo $municipal->id; ?>"><?php echo $municipal->first_name.' '.$municipal->last_name; ?></option>
                                        <?php else: ?>
                                            <option value="<?php echo $municipal->id; ?>"><?php echo $municipal->first_name.' '.$municipal->last_name; ?></option>
                                        <?php endif; ?>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                        <?php endfor;
                    elseif($municipal_number->meta_value == 9):
                        for ($x = 1; $x <= 9; $x++): ?>
                            <div class="col-lg-3">
                                <label>Select Municipal</label>

                                <select id="municipal_id_<?php echo $x; ?>" name="municipal_id[]" class="municipal_id form-control">
                                    <option value="0" selected>No Municipal</option>

                                    <?php foreach($municipals as $municipal): ?>
                                        <?php if(in_array($municipal->id, $ids)): ?>
                                            <option value="<?php echo $municipal->id; ?>"><?php echo $municipal->first_name.' '.$municipal->last_name; ?></option>
                                        <?php else: ?>
                                            <option value="<?php echo $municipal->id; ?>"><?php echo $municipal->first_name.' '.$municipal->last_name; ?></option>
                                        <?php endif; ?>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                        <?php endfor; ?>
                    <?php endif; ?>
                <?php endif; ?>

2 个答案:

答案 0 :(得分:2)

利用array_shift()函数和一个简单的function quillGetHTML(inputDelta) { var tempCont = document.createElement("div"); (new Quill(tempCont)).setContents(inputDelta); return tempCont.getElementsByClassName("ql-editor")[0].innerHTML; } 循环来实现您的逻辑,如下所示:

for

注意:注意$myArray = array('a','c'); for($i = 0; $i < 4; ++$i){ $flag = false; ?> <select id="id" name="id" class="form-control"> <option value="">No selection</option> <option value="a" <?php if(!$flag && in_array('a', $myArray)){ echo "selected='selected'"; $flag = true; } ?>>a</option> <option value="b" <?php if(!$flag && in_array('b', $myArray)){ echo "selected='selected'"; $flag = true; } ?>>b</option> <option value="c" <?php if(!$flag && in_array('c', $myArray)){ echo "selected='selected'"; $flag = true; } ?>>c</option> <option value="d" <?php if(!$flag && in_array('d', $myArray)){ echo "selected='selected'"; $flag = true; } ?>>d</option> </select> <?php array_shift($myArray); } id属性,每个元素都应该是唯一的。

答案 1 :(得分:0)

以下是一些示例代码:

$myArray = array('a','b','c','d');
$selected_values = array('a','c');
$number_of_selects = 4;

for ($i = 0; $i < $number_of_selects; $i++) {
    $sel_val = !empty($selected_values[$i])? $selected_values[$i] : false;?>
<select>
<?php
    foreach ($myArray as $v) {?>
        <option value="<?=$v?>" <?=$v===$sel_val? 'selected' : ''?>><?=$v?><option>
<?php
    }?>
</select>
<?php
}