Reputation: 243
I have some php which lists a bunch of options (through a loop) and prints them on the page with a new line for each.
I want the options to come up in a dynamically sized drop down HTML list each with different option values.
This is the PHP (it works perfectly):
if($postOpts) {
foreach($postOpts as $option)
echo $option["name"]." - ".$option["price"]."<BR>";
}
//Otherwise, present the default postage option for domestic or international.
else {
echo "Default Shipping Option - $".$post->getDefaultPrice();
}
I have a form but as you can see the form amount of options is static not dynamic:
<form> <!-- form action will go here -->
<div class="form-group col-xs-12">
<div class="col-xs-3 col-md-2 form-label"><label>Shipping Options:</label></div>
<div class="col-xs-4 col-md-3">
<select class="form-control" name="list" title="pick a type">
<!-- I assume I need to iterate through options here -->
<option value="01">Shipping1</option>
<option value="02">Shipping2</option>
<option value="03">Shipping3</option>
</select>
</div>
</div>
</form>
EDIT I forgot the other else statement in the PHP, please check.
Upvotes: 1
Views: 750
Reputation: 11987
Replace your select with this,
<select class="form-control" name="list" title="pick a type">
<?php if(count($postOpts)) { ?>
<?php foreach($postOpts as $row) { ?>
<option value="<?= $row["price"] ?>"><?= $row["name"] ?></option>
<?php } ?>
<?php } else { ?>
<option value="<?= $post->getDefaultPrice() ?>"><?= "Default Shipping Option - $".$post->getDefaultPrice() ?></option>
<?php } ?>
</select>
Upvotes: 1
Reputation: 1428
You can try this
<form> <!-- form action will go here -->
<div class="form-group col-xs-12">
<div class="col-xs-3 col-md-2 form-label"><label>Shipping Options:</label></div>
<div class="col-xs-4 col-md-3">
<select class="form-control" name="list" title="pick a type">
<option value="">Choose type</option>
<?php foreach($postOpts as $option) { ?>
<option value="<?php echo $option['name'].' - '.$option['price'];?>"><?php echo $option["name"]." - ".$option["price"]; ?></option>
<?php } ?>
</select>
</div>
</div>
</form>
Upvotes: 0