Pagination in php and ajax

Source: Internet
Author: User
I used php to write a paging program. today I tried to use ajax to implement this function. Haha, the code is not hard to write, but after writing the code, the test will find it, and then debug it for a while before it is correct. I am deeply impressed that most of the developer's time is spent on debugging code. I have summarized some fart experience.
1. use ajax post to post data to the background page, and then re-connect to the database. don't think you can use the previous session to connect.
2. to handle the returned garbled characters, I added the header ("Content-Type: text/html; charset = GB2312"); to display it normally. later, I checked it in firefox, I was prompted to download this webpage, and I searched a lot of information on the internet. I got a vague understanding that the webpage code has a syntax error. for security reasons, firefox does not display it directly but prompts to download it, I re-checked the statement just now and found that I wrote another "" and removed it to solve the problem. haha, so I encountered such a problem, check the html tag. after all, firefox is not as smart as ie.
3. finally, we should be responsible for web site developers. don't think everything is fine if you pass the test in ie. after all, not everyone uses ie, but you have to do more tests in other browsers, in this way, your professional level is displayed.

Ajax script:

The code is as follows:


Script
Function viewpage (p ){
If (window. XMLHttpRequest ){
Var xmlReq = new XMLHttpRequest ();
} Else if (window. ActiveXObject ){
Var xmlReq = new ActiveXObject ('Microsoft. xmlhttp ');
}
Var formData = "page =" + p;
XmlReq. onreadystatechange = function (){
If (xmlReq. readyState = 4 ){
Document. getElementById ('content2'). innerHTML = xmlReq. responseText;
}
}
XmlReq. open ("post", "pai_list.php", true );
XmlReq. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded ");
XmlReq. send (formData );
Return false;
}
Script


Call:

The code is as follows:



Header ("Content-Type: text/html; charset = GB2312 ");
$ Pagesize = 10;
// Echo $ _ POST ['Page'];
$ Result = mysql_query ("Select count (DISTINCT region name) FROM". TBL_HOTELS );
$ Myrow = mysql_fetch_array ($ result );
$ Numrows = $ myrow [0];

$ Pages = intval ($ numrows/$ pagesize );
If ($ numrows % $ pagesize)
$ Pages ++;
If (isset ($ _ POST ['Page']) {
$ Page = intval ($ _ POST ['Page']);
}
Else {
// Set it to the first page
$ Page = 1;
}
$ First = 1;
$ Prev = $ page-1;
$ Next = $ page + 1;
$ Last = $ pages;
// Calculate the record offset
$ Offset = $ pagesize * ($ page-1 );
// Read the specified number of records
$ Result = mysql_query ("select 'Partition name', count (*) from". TBL_HOTELS. "group by 'Partition name' order by id desc limit $ offset, $ pagesize ");
$ Num = mysql_num_rows ($ result );
While ($ row = mysql_fetch_array ($ result, MYSQL_NUM )){
$ Pipeline name [] = $ row [0];
$ Countpeople [] = $ row [1];
}
For ($ a = 0; $ a <$ num; $ a ++)
{
// $ Result = mysql_query ("select count (title) from ". TBL_Comments. "where 'title' = \"". $ title [$ a]. "\"");
// $ Row = mysql_fetch_row ($ result );
Echo"








\ N ";Echo" \ N ";Echo" \ N ";Echo" \ N ";Echo" \ N ";Echo" \ N ";Echo" \ N ";Echo"
\ N ";
// Rating_bar ($ title [$ a], 5 );
Echo"
$ Pipeline name [$ a] \ n ";
Echo"
\ N ";
Echo "recommended users: ($ countpeople [$ a]) | \ n ";
Echo"Average score:(". $ Count.") | number of comments :()\ N ";
Echo"
\ N ";
}
Echo"
Echo "border = 0> ";Echo"
";
Echo "";
Echo"

". $ Page." page/total ". $ pages." page | total ". $ numrows." | ";
If ($ page> 1) echo "homepage | ";
If ($ page> 1) echo "previous page | ";
If ($ page <$ pages) echo "next page | ";
If ($ page <$ pages) echo "last page ";
Echo "toPage";
Echo"

";

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.