在For循环中编写它

时间:2016-02-18 14:36:08

标签: php html for-loop

这是我尝试过的。

<option value="Label_0"<?php if (isset($format) && $name == "Label_0") { echo " selected"; } ?>><?php 
                        for($x = 0 ; $x < 24 ; $x++){
                        echo ( "Naam: " . $parcels[$x]['name'] . " || OrderID: ". ($parcels[$x]['order_number'])); 
                        }?></option>

我试过把它放在一个循环中:

<select name="format" id="format">
                    <option value="">Select One</option>
                    <optgroup label="Labels">
                        <option value="Label_0"<?php if (isset($format) && $name == "Label_0") { echo " selected"; } ?>><?php echo ( "Naam: " . $parcels[0]['name'] . " || OrderID: ". ($parcels[0]['order_number'])); ?></option>
                        <option value="Label_1"<?php if (isset($format) && $name == "Label_1") { echo " selected"; } ?>><?php echo ( "Naam: " . $parcels[1]['name'] . " || OrderID: ". ($parcels[1]['order_number'])); ?></option>
                        <option value="Label_2"<?php if (isset($format) && $name == "Label_2") { echo " selected"; } ?>><?php echo ( "Naam: " . $parcels[2]['name'] . " || OrderID: ". ($parcels[2]['order_number'])); ?></option>
                        <option value="Label_3"<?php if (isset($format) && $name == "Label_3") { echo " selected"; } ?>><?php echo ( "Naam: " . $parcels[3]['name'] . " || OrderID: ". ($parcels[3]['order_number'])); ?></option>
                        <option value="Label_4"<?php if (isset($format) && $name == "Label_4") { echo " selected"; } ?>><?php echo ( "Naam: " . $parcels[4]['name'] . " || OrderID: ". ($parcels[4]['order_number'])); ?></option>
                        <option value="Label_5"<?php if (isset($format) && $name == "Label_5") { echo " selected"; } ?>><?php echo ( "Naam: " . $parcels[5]['name'] . " || OrderID: ". ($parcels[5]['order_number'])); ?></option>
                        <option value="Label_6"<?php if (isset($format) && $name == "Label_6") { echo " selected"; } ?>><?php echo ( "Naam: " . $parcels[6]['name'] . " || OrderID: ". ($parcels[6]['order_number'])); ?></option>
                        <option value="Label_7"<?php if (isset($format) && $name == "Label_7") { echo " selected"; } ?>><?php echo ( "Naam: " . $parcels[7]['name'] . " || OrderID: ". ($parcels[7]['order_number'])); ?></option>
                        <option value="Label_8"<?php if (isset($format) && $name == "Label_8") { echo " selected"; } ?>><?php echo ( "Naam: " . $parcels[8]['name'] . " || OrderID: ". ($parcels[8]['order_number'])); ?></option>
                        <option value="Label_9"<?php if (isset($format) && $name == "Label_9") { echo " selected"; } ?>><?php echo ( "Naam: " . $parcels[9]['name'] . " || OrderID: ". ($parcels[9]['order_number'])); ?></option>
                        <option value="Label_10"<?php if (isset($format) && $name == "Label_10") { echo " selected"; } ?>><?php echo ( "Naam: " . $parcels[10]['name'] . " || OrderID: ". ($parcels[10]['order_number'])); ?></option>
                        <option value="Label_11"<?php if (isset($format) && $name == "Label_11") { echo " selected"; } ?>><?php echo ( "Naam: " . $parcels[11]['name'] . " || OrderID: ". ($parcels[11]['order_number'])); ?></option>
                        <option value="Label_12"<?php if (isset($format) && $name == "Label_12") { echo " selected"; } ?>><?php echo ( "Naam: " . $parcels[12]['name'] . " || OrderID: ". ($parcels[12]['order_number'])); ?></option>
                        </optgroup>
                    <input type="submit" value="Send" name "submit" />

我一直只获得最后一个选项值而不是全部。我没有注意到的任何问题?

1 个答案:

答案 0 :(得分:1)

喜欢这个吗?

<select name="format" id="format">
    <option value="">Select One</option>
        <optgroup label="Labels">
            <?php for($x=0; $x<24; $x++){ ?>
            <option value="Label_<?php print $x ?>"<?php if (isset($format) && $name == "Label_" . $x) { echo " selected"; } ?>><?php echo ( "Naam: " . $parcels[$x]['name'] . " || OrderID: ". ($parcels[$x]['order_number'])); ?></option>
            <?php } ?>
        </optgroup>
</select>
<input type="submit" value="Send" name "submit" />

我已更新答案,因为每个选项的值都相同。

Example of code running