This article mainly introduces the steps and methods of implementing ajax paging query. It has a good reference and value for learning ajax. Let's follow the editor to look at the detailed explanation of ajax paging query
(1) First write a page to display data. How many parts are needed for paging query?
1. First is the query text box input, and the query button, then start writing the code
2. The next step is to display the data. To display the data, you must check the database. How to use ajax
First introduce the jQuery package into the page that displays data
//Introducing jQuery package
Writing the contents of the columns you want to display, naturally you have to write a table, write a row, and put cells in the row The field name of the content you want to display (three types of information are displayed here)
//显示的字段名,这是第一行的内容
代号
名称
父级代号
Copy after login
I haven’t checked the database yet, but you can take a look at the display effect first:
3. Now you can check the database first, and ajax will be used here
3.1 But since If you want to display it in pages, there will be a default first page. You can first set a variable
var page = 1; //Current page
3.2 Then start writing ajax and query the database, but this will be used frequently. To avoid writing it many times, we can write a method
3.3 Then write the processing page for displaying data. What needs to be considered here is how many pieces of data to skip and how many pieces of data you want to display
$num = 20; //每页想要显示的数据条数 $tiao = ($page-1)*$num; //显示的当前跳过多少条数据 //查询表中模糊查询名称是关键字,分页是跳过多少条,显示多少条数据 $sql = "select * from chinastates where areaname like '%{$key}%' limit {$tiao},{$num}"; //执行sql语句 echo $db->JSONQuery($sql); //调用的是写好的JSON数据格式的处理方式
Copy after login
The JSON data format is an associative array, so you need to process it and encapsulate the processing method into a class
In "AJAX The processing method is written in "dataType (data format)-text, json"
3.4 After the processing of the page is completed, it is necessary to write the code after executing the processing page in ajax (note : The above uses the JSON data format, so please note that the field names must be the same as those in the database, and it is an associative array)
success: function(data){ var str = ""; for(var k in data) { //循环显示的代号、名称、父级代号 str +="
"+data[k].AreaCode+"
"+data[k].AreaName+"
"+data[k].ParentAreaCode+"
"; } $("#bg").html(str); //将内容放大显示这些数据的地方 }
Copy after login
In this way, the data you want to display is placed in the bg. Remember to call this method
The data is displayed at this point, but there is no way To implement paging, paging is also needed. Here we need to put numbers, but they also need to be traversed. You can just put them empty
//显示数字或是上一页
Copy after login
3.5 This can also be written as a method, and then call
To know the maximum number of pages that can be displayed, you can first define a default maximum number. This maximum number can also be used when searching for keywords. Maximum number of pages displayed
var maxys = 1;
Find the value of the key
var key = $("#key ").val();
Then write ajax and check the total number of pages
The above is the detailed content of Detailed explanation of ajax paging query. For more information, please follow other related articles on the PHP Chinese website!
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