2017-06-15 65 views
-3

控制器代碼更新代碼,我有:需要爲我的節目

<?php 
    class Latest_ctrl extends Ci_controller{ 


     public function insert(){ 

      $name=$this->input->post('name'); 
      $pass=$this->input->post('pass'); 
      $email=$this->input->post('email'); 
      $mobile=$this->input->post('mobile'); 
      $address=$this->input->post('address'); 
      $data=array(
       'name'=>$name, 
       'pass'=>$pass, 
       'email'=>$email, 
       'mobile'=>$mobile, 
       'address'=>$address 
       ); 
      $this->load->model('latest_model'); 
      $query= $this->db->insert('form',$data); 
      if($query){ 
       redirect('latest_ctrl/view'); 
      } 
       } 

     public function view(){ 
      $this->load->model('latest_model'); 
      $val=$this->latest_model->get_data(); 
       $data['value']=$val; 
       $this->load->view('latest',$data); 
     } 
     public function index(){ 
    $this->load->view('new_login'); 
     } 

     public function delete($id){ 
     $id=$this->uri->segment(3); 
     $this->load->model('latest_model'); 
      $this->latest_model->delete_id($id); 
      redirect(base_url('latest_ctrl/view')); 

     } 

     Public function update($id){ 
      $upd=$this->uri->segment(3); 
      $data = array(
      'name' => $this->input->post('name'), 
      'pass' => $this->input->post('pass'), 
      'email' => $this->input->post('email'), 
      'mobile' => $this->input->post('mobile'), 
      'address' => $this->input->post('address') 
      ); 
      $this->load->model('latest_model'); 

     } 
    } 
    ?> 

型號代碼我有:

<?php 
class Latest_model extends CI_model{ 


public function insert($tableName,$data){ 
     return $this->db->insert($tableName, $data); 
    } 

    public function get_data(){ 
     $query = $this->db->get('form'); 
     if ($query->num_rows() > 0) 
     { 
     $row=$query->result(); 
     return $row; 
     } 
    } 

    public function delete_id($id){ 
     $this->db->where('id', $id); 
     $this->db->delete('form'); 
    } 


public function update_data(){ 

} 
} 


?> 

觀點我有:

<!DOCTYPE html> 
<html> 
<head> 
    <title></title> 
</head> 
<body> 



<h1>Welcome Page </h1> 
<form action="<?php echo base_url();?>latest_ctrl/insert" method="post"> 
<table > 
<tr> 
    <td colspan=2 align="center"><h3>User Details</h3></td> 
</tr> 
<tr> 
    <td> 
     <?php echo form_label('Name'); ?> 
    </td> 
    <td> 
     <?php echo form_input(array('id' => 'name', 'name' => 'name','value'=>'')); ?> 
    </td> 
</tr> 
<tr> 
    <td> 
     <?php echo form_label('Pass'); ?> 
    </td> 
    <td> 
     <?php echo form_password(array('id' => 'pass', 'name' => 'pass')); ?> 
    </td> 
</tr> 
<tr> 
    <td><?php echo form_label('Email'); ?> 
</td> 
<td><?php echo form_input(array('id' => 'email', 'name' => 'email')); ?></td> 
</tr> 
<tr> 
    <td><?php echo form_label('Mobile'); ?> 
</td> 
    <td><?php echo form_input(array('id' => 'mobile', 'name' => 'mobile')); ?> 
</td> 
</tr> 
<tr> 
    <td> 
     <?php echo form_label('Address'); ?> 
    </td> 
    <td> 
     <?php echo form_input(array('id' => 'address', 'name' => 'address')); ?> 
    </td> 
</tr> 
<tr> 
    <td colspan="2" align="center"><?php echo form_submit(array('id' => 'submit', 'value' => 'Submit')); ?> 
</td> 
</tr> 
</form> 
</table> 

</body> 
</html> 

我想編輯特定的爲此目的,我需要模型和控制器代碼來更新codeigniter中的記錄。

此外,我有一個頁面,將頁面重定向到更新控制器,但我只想更新模型和控制器的代碼以進行更新。

+0

請看看本主題] (https://stackoverflow.com/help/on-topic) – hassan

回答

0
**Controller** 

- >添加構建和負荷模型在這裏,所以你不能在每個功能加載模型

<?php 
class Latest_ctrl extends CI_controller{ 

    function __construct() 
    { 
    parent::__construct(); 

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


} 

    public function insert() 
    { 

     $name=$this->input->post('name'); 
     $pass=$this->input->post('pass'); 
     $email=$this->input->post('email'); 
     $mobile=$this->input->post('mobile'); 
     $address=$this->input->post('address'); 
     $data=array(
      'name'=>$name, 
      'pass'=>$pass, 
      'email'=>$email, 
      'mobile'=>$mobile, 
      'address'=>$address 
     ); 

     $id = $this->latest_model->insert($tableName="form",$data); 

     if($id) 
     { 
      redirect('latest_ctrl/view'); 
     } 
    } 

    public function view() 
    { 

     $val=$this->latest_model->get_data(); 
     $data['value']=$val; 
     $this->load->view('latest',$data); 
    } 

    public function index(){ 
     $this->load->view('new_login'); 
    } 

    public function delete($id) 
    { 
     $id=$this->uri->segment(3); 

     $this->latest_model->delete_id($id); 
     redirect(base_url('latest_ctrl/view')); 

    } 

    Public function update($id) 
    { 
     $id=$this->uri->segment(3); 
     $data = array(
      'name' => $this->input->post('name'), 
      'pass' => $this->input->post('pass'), 
      'email' => $this->input->post('email'), 
      'mobile' => $this->input->post('mobile'), 
      'address' => $this->input->post('address') 
     ); 

    $this->latest_model->update_data($tableName="form",$data,$id); 
    } 
} 
?> 

型號代碼

<?php 
    class Latest_model extends CI_model{ 


    public function insert($tableName,$data) 
    { 
     $this->db->insert($tableName, $data); 
     return $this->db->insert('form',$data); 
    } 

    public function get_data() 
    { 
     $query = $this->db->get('form'); 
     if ($query->num_rows() > 0) 
     { 
     $row=$query->result(); 
     return $row; 
     } 
    } 

    public function delete_id($id) 
    { 
     $this->db->where('id', $id); 
     $this->db->delete('form'); 
    } 


    public function update_data($tableName,$data,$id) 
    { 
     $this->db->where('id'=>$id) 
     $this->db->update($tableName, $data); 
     return true; 
    } 
} 
?>