如何设置此数据以使用视图和模型

How to setup this data to work with a view and a model

我不确定如何使用模型、视图和控制器进行设置。我是 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;
    }
}

在 codeigniter 中使用模型:

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

然后您从 Model_Name class:

调用方法
$this->Model_Name->some_method($parametar1, $parametar2);

您在使用前没有加载模型。

有多种方法可以使用模型。

如果你准时需要它,你可以在你的函数中加载它:

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 不正确。

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