Home  >  Article  >  Backend Development  >  PHP – EasyUI DataGrid data retrieval method introduction_PHP tutorial

PHP – EasyUI DataGrid data retrieval method introduction_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:15:16823browse

EasyUI DataGrid is a DataGrid written in Jquery. It can be seen that it is a front-end Web UI technology. Generally, it is more common for people to use background languages ​​​​such as PHP to directly generate HTML syntax to display DataGrid when generating DataGrid. When operating on the DataGrid, the entire web page is regenerated after passing parameters to the backend.

EasyUI DataGrid supports two methods. One is, as mentioned above, the background server generates the displayed HTML and displays it on the front end. The other is to use AJAX to generate it, which simply feeds JSON format data to the front end. After the front end receives the data, it analyzes the data and uses JQuery to refresh the screen of this part of the DataGrid.

What we introduce here is the second method, which is done using AJAX technology. The advantage of this is that the three layers of data layer -> control layer -> display layer can be operated independently to achieve what I am doing. The spirit mentioned in the previous introduction to multi-level architecture design will not be like the old method of putting all HTML generation in PHP. As a result, PHP developers themselves need to have a deep understanding of front-end technologies such as HTML before they can develop. problem.

Doing this now brings another benefit, that is, your front-end UI can be replaced, but the back-end program does not need to be significantly modified. There are currently many JavaScript DataGrids that support JSON data format. You can also refer to the DataGrids provided by other companies and choose the most suitable one to use.

This is the introduction. Next, if you look at the code directly, you will have a better understanding of what I mean above:

First of all, you need to design the HTML UI interface and define which fields are to be displayed. Display names, etc. Regarding this part of the field definition, EasyUI DataGrid also provides it, using JavaScript to dynamically define it, and I am used to using HTML to define it directly, which is not complicated. In terms of division of labor later, it is also easier to directly hand it over to the Web Artists will operate directly.

This part focuses on URL settings.
DataGrid2.php

Copy code The code is as follows:







A Little Dragon easyUI datagrid








A little dragon easyUI datagrid url test



url="datagrid2_getdata.php" title="Load Data" pagination="true">








< /tr>

UNum User ID Password Birthday Nickname DBSTS





to define Backend interface for data acquisition
datagrid2_getdata.php
Copy code The code is as follows:

$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
$rows = isset($_POST ['rows']) ? intval($_POST['rows']) : 10;
$offset = ($page-1)*$rows;
$result = array();

$tablename = "STUser";
// ...
require_once(".dbDB_config.php");
require_once(".dbDB_class.php");

$ db = new DB();
$db->connect_db($_DB['host'], $_DB['username'], $_DB['password'], $_DB['dbname']);
$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);
?>

From the above, it can be seen that this is a very simple data acquisition action.
At the beginning, DataGrid will pass in two parameters,
$_POST['page']) Which page is it currently on?
$_POST['rows']) How many pieces of data should be displayed on each page

Then, use an array $result to store two pieces of information.
$result["total"] has several pieces of data
$result["rows"] stores the actual data array set
Finally, the $result array must be generated and output in JSON data format. After receiving it, the DataGrid will process and refresh the screen.

Later, you can go one step further and abstract datagrid2_getdata.php, that is, separate the data format processing part unique to EasyUI DataGrid from the database access part, and separate them into two independent parts. class to handle.

A good architecture and class design are actually generated by the accumulation of experience. They are constantly evolving and improving. The most important spirit of the original framework is that the division of labor for each Class must be clear and precise. , this is to cope with the above-mentioned problems and to continuously evolve the corresponding measures, so that it will be easier to make modifications and adjustments in the future.

Otherwise, it is more likely to become that you want to change but don’t know where to start, because once you make a change, there are dozens or even hundreds of programs waiting for you to modify together, thus extending the problem of stability. , that is to say, everyone is opposed to modifying the original system, because there are too many to be modified, and it is not enough to modify one less. If dozens of problems are modified together, even if they are all modified, who will test whether the modifications have been completed? Is it your job? user is here to help you test it. After thinking about it, it’s better to forget it and don’t change it anymore. Anyway, the system is still usable now.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326129.htmlTechArticleEasyUI DataGrid is a DataGrid written in Jquery. It can be seen that it is a front-end Web UI technology. Generally, everyone uses The more common way to generate DataGrid is to use background PHP etc...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn