Yasser Moussa Yasser Moussa - 2 months ago 20
PHP Question

distribute array values in multiple select box

I have an array as follows

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


I need to distribute some values of them saved already in database into different number of select boxes let's say 4 list boxes so these saved values will appear selected in the first list boxes and the remaining ones will be default and let's say saved values are a and c

so based on that the output should have 4 list boxes starting with a (Selected) and second list box c (Selected) and the other 2 list boxes should be remained default something like that

<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>


Edit
Here is my code where i loop to produce either 7 or 9 list boxes according to the passed value and inside i loop on
$municipals
to show all municipals and a municipal exists inside an array of the saved values in DB i need to show it in the first select box and so on

<?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; ?>

Answer

Make use of array_shift() function and a simple for loop to implement your logic, like this:

$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);
}

Note: Take care of id and name attributes, it should be unique for every element.