从数据库中获取选定的form_dropdown代码点火器

时间:2018-07-28 14:25:26

标签: codeigniter

我是Codeigniter的新手,我正在尝试从数据库中设置选定的值,

此代码无效,

<?php 

     $options = array(
      'one'  => 'one',
      'two'  => 'two',
     );

     $selected = $teach->number;

     echo form_dropdown('number', $options, $selected); 
?> 

但是当我尝试手动操作时,它会起作用。

<?php 

     $options = array(
      'one'  => 'one',
      'two'  => 'two',
     );

     $selected = 'one';

     echo form_dropdown('number', $options, $selected); 
?>

我不知道为什么, 任何建议都很好。谢谢


这是我的控制器

public function index() {
    $this->load->helper('url');

    $id = $this->uri->segment(3);
    $this->data['teach'] = $this->EditTeachModel->getPosts(); // calling Post model method getPosts()
    $this->data['singleTeach'] = $this->EditTeachModel->getPostsId($id); // calling Post model method getPosts()

    //view
    $this->load->view('edit_teach', $this->data); // load the view file , we are passing $data array to view file
}

public function update(){
    $id = $this->input->post('kd');
    $this->load->library('form_validation');

    $data = array(
        'kd' => $this->input->post('kd'),
        'name' => $this->input->post('name'),
        'date' => $this->input->post('date'),
        'number' => $this->input->post('number'),
        'pass' => $this->input->post('pass')
    );
    $this->EditTeachModel->updatePosts($id, $data); 

    $this->index();


}

这是我的模特

public function getPosts(){
    $this->db->from('teach');
    $query = $this->db->get();

    return $query->result();
}

public function getPostsId($data){
    $this->db->select('*');
    $this->db->from('teach');
    $this->db->where('kd', $data);
    $query = $this->db->get();
    $result = $query->result();
    return $result;
}

public function updatePosts($id, $data){
    $this->db->where('kd', $id);
    $this->db->update('teach', $data);
}

这是我的观点

<?php foreach($singleTeach as $teach) {?>
    <form method="post" action="<?php echo base_url() . "edit_teach/update"?>">
    <div class="card-body"><td>
       <div class="row">
          <div class="col-6">
            <div class="form-group">
              <label id="kd">Kode :</label>
              <input type="text" id="kd" name="kd" class="form-control" value="<?php echo $teach->kd; ?>">
            </div>
            <div class="form-group">
              <label id="name">Nama:</label>
              <input type="text" id="name" name="name" class="form-control" value="<?php echo $teach->name; ?>">
            </div>
            <div class="form-group">
              <label id="date">Date :</label>
              <input type="text" id="date" name="date" class="form-control" value="<?php echo $teach->date; ?>">
            </div>
            <div class="form-group">
              <?php echo form_label('Number '); ?> <?php echo form_error('number'); ?>
              <?php 
                  $options = array(
                  'one'         => 'one',
                  'two'         => 'two',
                  );

                  $selected = $teach->number;
                  print_r($teach);

                 echo form_dropdown('number', $options, $selected); 
                ?> 
             </div>
             <div class="form-group">
                <label id="pass">Password :</label>
                <input type="text" id="pass" name="pass" class="form-control" value="<?php echo $teach->pass; ?>">
             </div>
            </div>
             </div>
         </div>
        <!-- /.card-body -->

       <div class="card-footer">
          <?php echo form_submit(array('id' => 'submit','class'=> 'btn btn-primary', 'value' => 'Submit')); ?>
       </div>
      </form>
  <?php } ?>

0 个答案:

没有答案