Add, delete, modify, and query operations in the CI (CodeIgniter) framework. The code for copying the CodeIgniter data function class in systemdatabaseDB_active_rec.php is as follows: spanclassModelNameextendsCI_Model {function _ construct () CodeIgniter data function class in \ system \ database \ DB_active_rec.php
The code is as follows:
Class ModelName extends CI_Model
{
Function _ construct ()
{
Parent: :__ construct ();
}
}
Connect to the database: $ this-> load-> database ();
The code is as follows:
ClassModel_name extends CI_Model
{
Function _ construct ()
{
Parent: :__ construct ();
$ This-> load-> database ();
}
}
Written in the model constructor, so that the model can be loaded and connected to the database, which is very convenient.
Insert data
The code is as follows:
$ This-> db-> insert ($ tableName, $ data );
$ TableName = indicates the name of the table to be operated.
$ Data = the data you want to insert, which is inserted as an array (key name = field name, key value = field value, auto-incrementing primary key does not need to be written ).
Update data
The code is as follows:
$ This-> db-> where ('field name', 'Field value ');
$ This-> db-> update ('Table name', an array of modified values );
Query data
The code is as follows:
$ This-> db-> where ('field name', 'Field value ');
$ This-> db-> select ('field ');
$ Query = $ this-> db-> get ('Table name ');
Return $ query-> result ();
Delete data
The code is as follows:
$ This-> db-> where ('field name', 'Field value ');
$ This-> db-> delete ('Table name ');
Next, we need to call our model in the controller.
The code is as follows:
$ This-> load-> model ('Model name') // Model name refers to the model you created under the project directory/models/(same as the file name)
$ This-> model name-> method name
To avoid calling each controller method once. I did this.
The code is as follows:
Class ControllerName extends CI_Controller
{
Function _ construct ()
{
Parent: :__ construct ();
$ This-> load-> model ('Model name ');
}
}
Coding \ system \ database \ DB_active_rec.php code: span style = "font-size: 16px;" class ModelName extends CI_Model {function _ construct ()...