PHP-EasyUIDataGrid data storage method introduction _ php Basics-php Manual

Source: Internet
Author: User
Tags datagrid example php basics
Following the method of retrieving data from the PHP-EasyUIDataGrid file in the previous article, this article describes how to operate the DataGrid, store data to the database, and implement the MVC architecture, the separation and independent operation of the data layer follow the method of retrieving data from the PHP-EasyUI DataGrid in the previous article. This article continues to describe how to operate the DataGrid, store data to the database, and implement the MVC architecture, 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 integrate the four scattered programs into one program for calling. before readers continue reading, you can first learn how to obtain the data from the PHP-EasyUI DataGrid and how the official example Build CRUD Application with jQuery EasyUI works. at least you must Run the example. run is very important, don't just look at it. you can test it to understand the problems.

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

The code is as follows:

 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

The code is as follows:

   
  EasyUI datagrid 
  
  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.