2015-02-17 76 views
0

我遇到的問題不確定如何使用模型,視圖和控制器進行設置。我是codigniter的新手,有點失去了學習。我在控制器中設置了一切,我知道現在是錯誤的,當我點擊提交後插入一條記錄時,頁面出現頁面無法顯示。當它應該只是將我重定向到我插入記錄。如何設置此數據以使用視圖和模型

員工控制器

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

    class Employee extends CI_Controller { 
    function __construct() 
{ 
    parent::__construct(); 
    $this->load->model('employee_model'); 

    } 

    //Insert the employee 
     public function insert_employee() 
     { 


      $data=array('name'=>$this->input->post('name'), 
       'LanId'=>$this->input->post('LanId'), 
       'reason'=>$this->input->post('reason'), 
       'PepNumber'=>$this->input->post('PepNumber'), 
       'Employee_Number'=>$this->input->post('Employee_Number'), 
       'department'=>$this->input->post('department'), 

       'status'=>1); 
      //print_r($data); 

      $result=$this->employee_model->insert_employee($data); 
      if($result==true) 
      { 
       $this->session->set_flashdata('msg',"Employee Records Added Successfully"); 
       redirect('employee/index'); 

      } 
      else 
      { 

       $this->session->set_flashdata('msg1',"Employee Records Added Failed"); 
       redirect('employee/index'); 


      } 
     } 

員工模型

<?php 

class Employee_model extends CI_Model 
{ 

    public function insert_employee($data) 
    { 
     $this->db->insert('employee_list',$data); 
     return ($this->db->affected_rows() != 1) ? false:true; 
    } 
    public function get_employee() 
    { 
     $this->db->select('*'); 
     $this->db->from('employee_list'); 
     $this->db->where('status',1); 

     $query =$this->db->get(); 
     return $query->result(); 
    } 
    public function delete_employee($id,$data) 
    { 
     $this->db->where('id',$id); 
     $this->db->update('employee_list',$data); 
     return ($this->db->affected_rows() != 1) ? false:true; 
    } 
    public function edit_employee($id) 
    { 
     $this->db->select('*'); 
     $this->db->from('employee_list'); 
     $this->db->where('id',$id); 
     $this->db->where('status',1); 
     $query =$this->db->get(); 
     return $query->result(); 

    } 
    public function update_employee($data,$id) 
    { 
     $this->db->where('id',$id); 
     $this->db->update('employee_list',$data); 
     return ($this->db->affected_rows() != 1) ? false:true; 
    } 
} 
+0

除非你已經做了,在構造函數或自動加載,你必須在使用前加載模型:'$這個 - >負載>模型(「employee_model」);' – AdrienXL 2015-02-17 20:26:06

回答

0

在使用它之前,您沒有加載模型。

有多種方式來處理模型。

如果你需要準時,你可以加載在你的函數:

public function myFunction() 
{ 
    $this->load->model("mymodel"); 
    $this->mymodel->myfunction(); 
} 

但是,如果它在你的控制器一種很常見的模式,可以爲所有在構造函數加載一次:

class MyController extends CI_Controller 
{ 

    public function __construct() 
    { 
     parent::__construct(); 
     $this->load->model('mymodel'); 
    } 

    public function index() 
    { 
     $this->mymodel->myfunc(); 
    } 
} 

最後,如果它是應用程序的中心模型,則可以爲您的所有控制器加載它。

進入的application/config/autoload.php:

$autoload['model'] = array("mymodel"); 

編輯:另外,你的模型的迴歸是不正確的。

return $this->db->affected_rows(); //It's enought :) 
+0

這是所有寫在控制器中是不正確的 – Veronica 2015-02-17 21:07:51

+0

我沒有在你提供的代碼中看到'load-> model()'。 – AdrienXL 2015-02-17 21:09:04

+0

剛剛添加了模型 – Veronica 2015-02-17 21:09:50

0

與模型相關工作笨:

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

而且你打電話從MODEL_NAME類方法:

$this->Model_Name->some_method($parametar1, $parametar2);