/**
* 使用一个表中的数据制作下拉框
*
*/
function buildSelect($tableName, $selectName, $valueFieldName, $textFieldName, $selectedValue = '')
{
$model = D($tableName);
$data = $model->field("$valueFieldName,$textFieldName")->select();
$select = "<select name='$selectName'><option value=''>请选择</option>";
foreach ($data as $k => $v)
{
$value = $v[$valueFieldName];
$text = $v[$textFieldName];
if($selectedValue && $selectedValue==$value)
$selected = 'selected="selected"';
else
$selected = '';
$select .= '<option '.$selected.' value="'.$value.'">'.$text.'</option>';
}
$select .= '</select>';
echo $select;
}
添加时
<tr>
<td class="label">所在品牌:</td>
<td>
<?php buildSelect('brand','brind_id','id','brand_name'); ?>
</td>
</tr>
修改时
<tr>
<td class="label">所在品牌:</td>
<td>
<?php buildSelect('brand','brind_id','id','brand_name',$data['brand_id']); ?>
</td>
</tr>