更新codeigniter中的特定数据库记录

时间:2015-11-26 02:07:39

标签: php mysql codeigniter

美好的一天,我在数据库中选定的行上制作更新特定记录时遇到一些麻烦。我想在点击表格显示表中所选数据的编辑图标后显示数据。 enter image description here

控制器:

//Show Users/Members page
public function users_page() {
  $data['users'] = $this->user->get_users();
  $this->load->view('user_page' , $data);
}

型号:

// Retrieve user data from database
public function get_users() {
  $this->db->select('id, email, first_name, last_name, date_registered');
  $this->db->from('auth_users');
  $query = $this->db->get();

  $result = $query->result();

foreach ($result as $key => $value) {
// var_dump($key);
$data[$key] = array(
    'id'  => $value->id,
    'firstname' => $value->first_name,
    'lastname' => $value->last_name,
    'datereg' => $value->date_registered,
    'email' => $value->email
    );
}
    return $data;
} 

查看:

        <table class="table table-condensed">
      <thead>
        <tr>
          <th>Firstname</th>
          <th>Lastname</th>
          <th>Email</th>
          <th>Date Registered</th>
          <th>Action</th>
        </tr>
        <?php foreach($users as $key => $value) {?>
          <tr>
            <td><?php echo $value['firstname']; ?></td>
            <td><?php echo $value['lastname']; ?></td>
            <td><?php echo $value['email']; ?></td>
            <td><?php echo $value['datereg']; ?></td>
            <td><a href="<?php echo site_url('main/update/'.$value['id']); ?>"><i class="material-icons" title="Click to update">mode_edit</i></a> |
             <a href="<?php echo site_url('main/delete_row/'.$value['id']); ?>"><i class="material-icons" title="Click to remove">delete</i></a></td>
          </tr>
          <?php } ?>
      </thead>
      <tbody>

      </tbody>
    </table>

1 个答案:

答案 0 :(得分:2)

  1. 控制器中创建一个新方法,就像这样(不完全像我不知道你已经使用过的类)

    public function show_user($id) {
      $data['users'] = $this->user->get_user($id);
      $this->load->view('user_page' , $data);
    }
    
  2. 模型中创建另一种方法,再次类似

    public function get_user($id) {
      $this->db->select('id, email, first_name, last_name, date_registered');
      $this->db->from('auth_users');
      $this->db->where('id', $id); 
      $query = $this->db->get();
    
      $result = $query->result();
      $var_dump($result); --or write a view an pass the result to the view
    
  3. 创建一个表格,取得前一个结果(我不能为你写)

  4. 控制器模型中创建更新方法(就像您刚查询信息一样,但现在更新此记录中的信息)