$row['kind'] = isset($row['kind']) ? $row['kind']: ""; $row['kind_options'] = get_kind_options($row['kind']);程式 - 函數(取得選項,傳入預設值,selected 代表選擇)
function get_kind_options($default=""){ global $db; $kind_options = array( "1"=>"類別1", "2"=>"類別2", "3"=>"類別3", "4"=>"類別4", "5"=>"類別5" ); $options = ""; foreach($kind_options as $key=> $option){ $selected = ($default == $key) ? " selected":""; $options .="<option value='{$key}'{$selected}>{$option}</option>"; } return $options; }樣板
<div class="col-sm-3"> <div class="form-group"> <label for="kind">類別</label> <select name="kind" class="form-control" size="1"> <{$row.kind_options}> </select> </div> </div>
<div class="col-sm-3"> <div class="form-group"> <label for="price">商品價格</label> <input type="text" class="form-control" id="price" name="price" value="<{$row.price}>"> </div> </div>
<div class="col-sm-3"> <div class="form-group"> <label for="amount">商品數量</label> <input type="text" class="form-control" id="amount" name="amount" value="<{$row.amount}>"> </div> </div>