how to use multi select in array

Viewed 38

I have a table and I put the values in it with an array I want to add a select tag that takes the values in each row of the table from the array and displays them in the select and sends the new values through the post method. please help

<?php for ($i = 0; $i < count($pays_percents); $i++) { ?>
<td>
    <select class="form-control" name="pay_method[]" id="pay_method">
    <option value="0" <?php if ($pay_method[$i] == '0') echo 'selected=""'; ?>>نقد</option>
    <option value="1" <?php if ($pay_method[$i] == '1') echo 'selected=""'; ?>>چک</option>
    <option value="2" <?php if ($pay_method[$i] == '2') echo 'selected=""'; ?>>کارت خوان</option>
    <option value="3" <?php if ($pay_method[$i] == '3') echo 'selected=""'; ?>>کارت به کارت</option>
    </select>
</td>
<?php } ?>
1 Answers

It is unclear what you are expecting. Here are the fixes for the flaws in the code. In particular, ids and names must be unique, so I have appended the iterator. And you don't need to say selected=''.

<?php for ($i = 0; $i < count($pays_percents); $i++) { ?>
<td>
    <select class="form-control" name="pay_method<?php echo $i;?>" id="pay_method<?php echo $i;?>">
    <option value="0" <?php if ($pay_method[$i] == '0') echo 'selected'; ?>>نقد</option>
    <option value="1" <?php if ($pay_method[$i] == '1') echo 'selected'; ?>>چک</option>
    <option value="2" <?php if ($pay_method[$i] == '2') echo 'selected'; ?>>کارت خوان</option>
    <option value="3" <?php if ($pay_method[$i] == '3') echo 'selected'; ?>>کارت به کارت</option>
    </select>
</td>
<?php } ?>
Related