Add and Remove Check Operation in CI of CodeIgniter Framework

  • 2021-06-28 11:55:12
  • OfStack

CodeIgniter's data function class is in systemdatabaseDB_active_rec.php


<span style="font-size:16px;">class ModelName extends CI_Model 
{ 
    function __construct() 
    { 
        parent::__construct(); 
    } 
}</span>

Connect to database: $this- > load- > database();


<span style="font-size:16px;">classModel_name extends CI_Model 
{ 
    function __construct() 
    { 
        parent::__construct(); 
        $this->load->database(); 
    } 
}</span>

Write it in the model's constructor so that you can load the model while connecting to the database, which is very convenient.


insert data


<span style="font-size:16px;">$this->db->insert($tableName,$data);</span>
$tableName =  Is the name of the table you want to operate on.  
$data= The data you want to insert is inserted as an array (key name) = Field name, key value = Field value, self-increasing primary key not written ) . 

Update Data


<span style="font-size:16px;">$this->db->where(' Field name ',' field value '); 
$this->db->update(' Table Name ' , modify the array of values );</span>

Query Data


<span style="font-size:16px;">$this->db->where(' Field name ',' field value '); 
$this->db->select(' field '); 
$query= $this->db->get(' Table Name '); 
return$query->result();</span>

Delete data


<span style="font-size:16px;">$this->db->where(' Field name ',' field value '); 
$this->db->delete(' Table Name ');</span>

The next step is to invoke our model in the controller


<span style="font-size:16px;">$this->load->model(' Model name ')// The model name means you are <span style="color: rgb(255, 0, 0); "> Project Directory /models/</span> Underneath Model( Same as file name ) 
$this-> Model name -> Method Name </span>

In order not to call once in each controller's method.This is how I do it


<span style="font-size:16px;"> 
class ControllerName extends CI_Controller 
{ 
    function __construct() 
    { 
        parent::__construct(); 
        $this->load->model(' Model name '); 
    } 
}</span>


Related articles: