这个例子是针对我们应用中的一个国家表,该表包含,id,symbol,name
控制器:
$data['countries']=$this->countries_model->get_countries();
$this->load->view('countries',$data);
模型:
function get_countries()
{
$query = $this->db->get('countries');
if ($query->num_rows >= 1)
{
foreach($query->result_array() as $row)
{
$data[$row['countryId']]=$row['countryName'];
}
return $data;
}
}
它返回一个数组,如:
$id=>$name
0=>Canada
1=>United States
看法:
echo form_dropdown('countries',$countries); //ci syntax
或者:
<select name="country">
<?php
foreach($countries as $country)
{
echo '<option value="'.$country['id'].'">'.$country['name'].'</option>';
}
?>
</select>