我是codeigniter的新手,我在从数据库检索数据到下拉列表时遇到问题。任何人都可以帮我这个吗?
我的观点:
<?php echo form_open('form/myform'); ?>
<select id="addother" >
<option value="none" selected="selected"> ------Select School------ </option>
<?php foreach($groups as $row) {
echo '<option value="'.$row->id.'">'.$row->name.'</option>';
} ?>
</select>
<div id="addother">
<?php echo form_input(array('id'=>'addother_input', 'name'=>'school', 'placeholder'=>'Enter name of school...')); ?>
<input type="submit" id="add" name="submit" value="+" />
</div>
<?php echo form_close(); ?>
我的控制器:
function myform(){
$data['title'] = "myform";
$this->load->library('form_validation');
$this->load->model('school_model');
if($this->input->post()){
$sdata['school'] = $this->input->post('school');
$this->school_model->addItem($sdata);
}
$data['groups'] = $this->school_model->getAll();
$this->load->view('myform', $data);
}
我的模特:
function getAll() {
$query = $this->db->get('tblschool');
return $query->result();
}
function addItem($sdata){
return $this->db->insert('tblschool', $sdata);
}
我无法从数据库中检索数据到下拉列表中。我将衷心感谢您的帮助。谢谢!
答案 0 :(得分:0)
我认为您可以在&#34; myForm&#34;中提交表单时保留选择值。方法。因此,在同一方法中,检索新的组值。试试这个:
function myform(){
$data['title'] = "myform";
$this->load->library('form_validation');
$this->load->model('school_model');
if ($this->form_validation->run() == TRUE){
$sdata['school'] = $this->input->post('school');
$this->school_model->addItem($sdata);
}
$data['groups'] = $this->school_model->getAll();
$this->load->view('myform', $data);
}