Following the previous article PHP? How to obtain the EasyUIDataGrid data? L small? Continue to explain how to operate the DataGrid, store data to the database, implement the MVC architecture, and separate and operate the data layer independently.
Following the previous article PHP? EasyUI DataGrid data retrieval method. This article continues to describe how to operate the DataGrid, store data to the database, implement the MVC architecture, and separate and operate the data layer independently.
This article focuses on improvements. The original EasyUI DataGrid example builds CRUD Application with jQuery EasyUI.
The official example shows how to operate the materials, but the problem is that each action you want to perform requires a corresponding program to operate the materials, for example, to add, delete, modify, and obtain information, a total of at least four corresponding programs are required to operate.
Readers can think about it. this is only the maintenance of the basic information of a single-file user. Generally, there are dozens or even dozens of programs running on the basic information of the system, it is necessary to make improvements in order to operate in practice.
In the spirit of building a multi-level architecture design preface, we can find that these four programs are similar to each other in terms of basic data operations, so we can standardize them, use a fixed framework for later use by similar programs.
This part will be divided into several articles to gradually complete this process. with this gradual evolution process, we will learn how the framework is formed.
First, this article introduces how to turn the four scattered programs into one for calling. before readers can read them, they can first understand PHP? The EasyUI DataGrid data acquisition method and the official example Build CRUD Application with jQuery EasyUI operation method should at least Run the example. The run action is very important, you can test the problem to understand the problem.
To change the four programs into one program, the key is to modify the call url for each operation and change the dal_user.php program for Chengdu to call the DAL, next, you need to pass a type parameter before calling to tell dal what action you want to perform.
Currently, type defines the following four actions:
Add
Mod modification
Del delete
Obtain data
You can start writing the dal program after knowing what you want to do with the dal program. of course, the dal program is still a non-standard program, but he has already achieved the MVC spirit, the data access layer is separated from the presentation layer. later articles will introduce how to standardize the dal and UI presentation layers by using the program described in this article.
Dal_user.php
Copy codeThe code is as follows:
$ Result = false;
If (! Empty ($ _ REQUEST ['type'])
{
Require_once (".. \ db \ DB_config.php ");
Require_once (".. \ db \ DB_class.php ");
$ Db = new DB ();
$ Db-> connect_db ($ _ DB ['host'], $ _ DB ['username'], $ _ DB ['password'], $ _ DB ['dbname']);
$ Tablename = "STUser ";
$ Type = $ _ REQUEST ['type'];
If ($ type = "del ")
{
$ Id = $ _ REQUEST ['id'];
$ SQL = "delete from STUser where UNum = $ id ";
$ Result = $ db-> query ($ SQL );
} Else if ($ type = "data "){
$ Page = isset ($ _ POST ['Page'])? Intval ($ _ POST ['Page']): 1;
$ Rows = isset ($ _ POST ['rows '])? Intval ($ _ POST ['rows ']): 10;
$ Offset = ($ page-1) * $ rows;
$ Result = array ();
$ Db-> query ("select count (*) As Total from $ tablename ");
$ Row = $ db-> fetch_assoc ();
$ Result ["total"] = $ row ["Total"];
$ Db-> query ("select * from $ tablename limit $ offset, $ rows ");
$ Items = array ();
While ($ row = $ db-> fetch_assoc ()){
Array_push ($ items, $ row );
}
$ Result ["rows"] = $ items;
Echo json_encode ($ result );
} Else {
$ STUID = $ _ REQUEST ['stuid'];
$ Password = $ _ REQUEST ['password'];
$ Nickname = $ _ REQUEST ['nickname'];
$ Birthday = $ _ REQUEST ['birthday'];
If (! Empty ($ _ REQUEST ['id']) {
$ Id = $ _ REQUEST ['id'];
$ SQL = "update $ tablename set STUID = '$ stuid', Password =' $ password', Nickname = '$ nickname' where UNum = $ id ";
} Else {// is add
$ SQL = "insert into $ tablename (STUID, Password, Nickname, DBSTS) values ('$ stuid',' $ password', '$ nickname', 'A ')";
}
$ Result = $ db-> query ($ SQL );
}
}
If ($ type! = "Data ")
{
If ($ result = "true "){
Echo json_encode (array ('success' => true ));
} Else {
Echo json_encode (array ('MSG '=> 'had errors occured.'. $ result ));
}
}
?>
After the dal data access layer is defined, you can implement the UI interface to call the dal, because AJAX is used to access data, therefore, part of the control layer in MVC is placed in the interface layer. later, we can use JavaScript to standardize the control layer and transmit parameter calls through the php backend, in this way, all the control power is concentrated in a program. these articles will be introduced later.
Datagrid. php
Copy codeThe code is as follows:
EasyUI datagrid