Example of refreshing data pagination using php + ajax

Source: Internet
Author: User

The index. php file is shown in the following figure.

The code is as follows: Copy code

<? Php
Header ("Content-type: text/html; charset = GBK"); // output encoding to avoid Chinese garbled characters
?>
<Html>
<Head>
<Title> ajax paging demonstration </title>
<Script language = "javascript" src = "ajaxpg. js"> </script>
<Link rel = "stylesheet" type = "text/css" href = "page.css">
</Head>
<Body>
<Div id = "result">
<? Php
$ Page = isset ($ _ GET ['Page'])? Intval ($ _ GET ['Page']): 1; // obtain the value of the page in page = 18. If there is no page, the page number is 1.
$ Num = 3; // 10 data entries per page


$ Db = mysql_connect ("localhost", "root", "123456"); // create a database connection
Mysql_select_db ("demo", $ db) or die ("database link error"); // select the database to operate
Mysql_query ("set names gbk ");
/*
First, we need to obtain the actual amount of data in the database to determine the specific number of pages to be divided. The specific formula is
Total databases divided by the number of entries displayed on each page, more than one.
That is to say, 10/3 = 3.3333 = 4 there is a remainder, and we need to enter.
*/


$ Result = mysql_query ("select * from brand ");
$ Total = mysql_num_rows ($ result); // query all data


$ Url = 'Test. php '; // Set the URL of the ajax submit page address. Set it to test. php uses ajax to pass parameters to test. php then assigns the processed content to the div id = result on this page.

// Page number calculation
$ Pagenum = ceil ($ total/$ num); // obtain the total number of pages, which is also the last page.
$ Page = min ($ pagenum, $ page); // Get the homepage
$ Prepg = $ page-1; // Previous page
$ Nextpg = ($ page = $ pagenum? 0: $ page + 1); // Next page
$ Offset = ($ page-1) * $ num; // obtain the value of the first parameter of limit. If the first page is (1-1) * 10 = 0, the second page is (2-1) * 10 = 10.
$ Pagenav = "<ul> ";


// Start the paging navigation bar code:
$ Pagenav. = "<li> Display No. <B>". ($ total? ($ Offset + 1): 0 ). "</B>-<B> ". min ($ offset + 10, $ total ). "</B> records </li> <li> total $ total Records & nbsp; </li> ";


// If there is only one page, the function will jump out:
If ($ pagenum <= 1) return false;


$ Pagenav. = "<li> <a href = javascript: dopage ('result', '$ url? Page = 1');> homepage </a> </li> ";
If ($ prepg) $ pagenav. = "<li> <a href = javascript: dopage ('result', '$ url? Page = $ prepg ');> Previous page </a> </li> "; else $ pagenav. =" <li> Previous page </li> ";
If ($ nextpg) $ pagenav. = "<li> <a href = javascript: dopage ('result', '$ url? Page = $ nextpg ');> Next page </a> </li> "; else $ pagenav. =" <li> Next page </li> ";
$ Pagenav. = "<li> <a href = javascript: dopage ('result', '$ url? Page = $ pagenum ');> last page </a> </li> ";
$ Pagenav. = "<li> page $ </li> <li> $ pagenum page </li> </ul> ";


// If the input page number parameter is greater than the total page number, the error message is displayed.
If ($ page> $ pagenum ){
Echo "Error: Can Not Found The page". $ page;
Exit;
}
?> </Div> <div id = "results">
<? Php
Echo $ pagenav; // output paging navigation
?>
</Div>
</Body>
</Html>

Css code

/* CSS Document */
/* CSS Document */
# Result ul li {
Height: 20px;
Width: auto;
Display: block;
Color: #999;
Border: 1px solid #999;
Float: left;
List-style: none;
Font-size: 12px;
Margin-left: 5px;
Line-height: 20px;
Vertical-align: middle;
Text-align: center;
}
# Result ul li a: link {
Width: 50px;
Height: 20px;
Display: block;

Line-height: 20px;
Background: # 09C;
Border: 1px solid # fff;
Color: # fff;
Text-decoration: none;
}
# Result ul li a: hover {
Width: 50px;
Height: 20px;
Display: block;

Line-height: 20px;
Background: # 09C;
Border: 1px solid # fff;
Color: # F60;
Text-decoration: none;
}

The ajaxpg. js file is as follows:

// JavaScript Document
Var http_request = false;
Function send_request (url) {// initialization, specifying the processing function and sending the request function
Http_request = false;
// Start initializing the XMLHttpRequest object
If (window. XMLHttpRequest) {// Mozilla browser
Http_request = new XMLHttpRequest ();
If (http_request.overrideMimeType) {// sets the MIME category
Http_request.overrideMimeType ("text/xml ");
}
}
Else if (window. ActiveXObject) {// IE browser
Try {
Http_request = new ActiveXObject ("Msxml2.XMLHttp ");
} Catch (e ){
Try {
Http_request = new ActiveXobject ("Microsoft. XMLHttp ");
} Catch (e ){}
}
 }
If (! Http_request) {// exception. An error occurred while creating the object instance.
Window. alert ("An error occurred while creating the XMLHttp object! ");
Return false;
}
Http_request.onreadystatechange = processrequest;
// Determine the request sending method, URL, and whether to execute the following code synchronously
Http_request.open ("GET", url, true );
Http_request.send (null );
  }
// Function for processing the returned information
Function processrequest (){
If (http_request.readyState = 4) {// judge the object status
If (http_request.status = 200) {// The message is returned successfully. Start to process the information.
Document. getElementById ("results"). style. display = "none ";
Document. getElementById (reobj). innerHTML = http_request.responseText;
}
Else {// The page is abnormal.
Alert ("the page you requested is abnormal! ");
}
}
  }
Function dopage (obj, url ){
Document. getElementById (obj). innerHTML = "<font color = 'green' font-size = '12'> reading data... </font> ";
Send_request (url );
Reobj = obj;
}
 

The database files are as follows:

-- PhpMyAdmin SQL Dump
-- Version 2.8.1
Http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation date:, January 1, January 22, 2010
-- Server version: 5.0.22
-- PHP version: 5.2.12
--
-- Database: 'demo'
--

----------------------------------------------------------

--
-- Table structure 'brand'
--

Create table 'brand '(
'Id' int (7) not null auto_increment,
'Sp _ brand' varchar (255) default NULL,
Primary key ('id ')
) ENGINE = InnoDB default charset = utf8 AUTO_INCREMENT = 19;

--
-- Export the data 'brand' in the table'
--

Insert into 'brand' ('id', 'sp _ brand') VALUES (1, 'Hello World '),
(2, 'Hello '),
(3, 'n '),
(4, 'fdsafdsafdsa '),
(5, 'fdsafdafdsafdas '),
(6, 'fdsafdsa '),
(7, 'fdsafdsafdas '),
(8, 'n '),
(9, 'n '),
(10, 'n '),
(11, 'en 11 '),
(12, 'n '),
(13, 'n '),
(14, 'n '),
(15, 'n '),
(16, 'n '),
(17, 'n '),
(18, 'en 18 ');


The following describes the functions of these files.


Ajaxpg. js: The core file of ajax does not need to be modified.

Index. php: implements ajax without refreshing files. Here, the ajaxpg. js file is called, and the mysql User password is configured, which must be consistent with your local file and display the page effect.

Page.css: this is a paging CSS style file, which is used for beautification.

Brand. SQL: this is the MYSQL database file. Import it to the MYSQL database. Similarly, if it is not imported, you can refer to how to import. SQL articles in PHPfensi.com.

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.