使用codeigniter从数据库检索数据到下拉列表

时间:2014-11-28 22:44:19

标签: php database codeigniter

我已经可以将数据存入数据库,但我无法将其从数据库中检索到下拉列表中。任何人都可以帮助我吗?

我的观点:

<body>

 <div id="container">


      <?php echo form_open('form/myform'); ?>

       <select class="form-control" >
        <?php 

        foreach($groups as $row)
        { 
          echo '<option value="'.$row->school.'">'.$row->school.'</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(); ?>
   </div>

我的控制器:

    function myform(){
    $data['title'] = "myform";

    $this->load->library('form_validation');

    $this->load->model('school_model');

    $sdata['school'] = $this->input->post('school');

    $this->school_model->addItem($sdata);

    $this->load->view('myform');

}

function drop() {
    $this->load->model('school_model');

    $data['groups'] = $this->school_model->getAll();

    $this->load->view('myform', $data);
}

我的模特:

 function getAll() {

   $query = $this->db->query('SELECT school FROM tblschool');

   return $query->result();

}


function addItem($sdata){

   return $this->db->insert('tblschool', $sdata);

}

我真的很困惑如何从数据库中检索数据到下拉列表中。我非常感谢你的帮助。

1 个答案:

答案 0 :(得分:0)

您是否正在访问form/drop以生成视图?如果您想通过form/myform访问此内容,则必须更改myform以包含对$this->school_model->getAll();的调用

public 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);
}