从数据库填充选项列表

时间:2013-06-02 04:25:27

标签: php arrays codeigniter foreach

在我的数据库表row()中,我在'product_size'列中有“24,26,28,30”的大小。

查询后,我想在viewProduct.php页面中查看:

<'option value=" ">' size <'/option>`<br />
<'option value="24"> 24 <'/option>`<br />
<'option value="26"> 26 <'/option>`<br />
<'option value="28"> 28 <'/option>`<br />
<'option value="30"> 30 <'/option><br/>

foreach()循环一样。

2 个答案:

答案 0 :(得分:0)

此代码是您所需要的:

<select name="product[location_id]">
<?php
foreach($product_locations as $product_location) :
    echo "<option value=\"".$product_location['id']."\" ".($product_location['id'] == $product['product_location'] ? "selected=\"yes\"" : "").">".$product_location['title']."</option>";
endforeach;
?>
</select>

当然,您需要将其调整到您的表/列,因为您没有提供任何有关它的信息。

答案 1 :(得分:0)

使用爆炸方法;

$array=explode(",",$row['product_size']);
for($i=0;$i<count($array);$i++) {
  echo $array[$i];
  enter code here
}