我是 CodeIgniter 的新手,并且正在处理我有表单的应用程序。在这种形式我有一个下拉列表。我在数据库列中有一些值。我想在下拉列表中显示这些值,我不知道该怎么做。
我的观点是:
<tr>
<td>Moderator :</td>
<td><label for="select"></label>
<select name="mod" tabindex="1" >
<!--values i want from database-->
</select>
</td>
</tr>
答案 0 :(得分:1)
此示例适用于我们的应用程序中的国家/地区表,该表包含,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>
答案 1 :(得分:0)
<?php
class Member_model extends CI_Model{
var $table = 'tbl_member';
public function __construct(){
parent::__construct();
$this->load->database();
}
public function get_all_members(){
$this->db->from('tbl_member');
$query=$this->db->get();
return $query->result();
}
public function get_by_id($id){
$this->db->from($this->table);
$this->db->where('member_id',$id);
$query = $this->db->get();
return $query->row();
}
public function member_add($data){
$this->db->insert($this->table, $data);
return $this->db->insert_id();
}
public function member_update($where, $data){
$this->db->update($this->table, $data, $where);
return $this->db->affected_rows();
}
public function delete_by_id($id){
$this->db->where('member_id', $id);
$this->db->delete($this->table);
}
}