Ajax Paging Code

Source: Internet
Author: User

show.php

<meta charset= "Utf-8" &GT;&LT;?PHP//connecting to a database$link=Mysqli_connect(' 127.0.0.1 ', ' root ', ' root ', ' mone ');//Set character setsMysqli_query($link, ' Set names UTF8 ');//perform paging operations//receive the current page (if it is empty, we will use the first page, if not empty, we will use the received value)$page=Empty($_get[' page '])? 1:$_get[' Page '];//find the total number of bars$sql= "SELECT * from User";$res=Mysqli_query($link,$sql);$count=mysqli_num_rows($res);//number of bars to display per page$length= 3;//Total Pages$zong _page=Ceil($count/$length);//Offset Amount$limit= ($page-1) *$length;//Enquiry$sql 2= "SELECT * FROM User Limit"$limit,$length";$res 2=Mysqli_query($link,$sql 2); while($a=Mysqli_fetch_assoc($res 2)) {    $data[] =$a;}?><table> <tr> <th>ID</th> <th> name </th> <th> password </th&        Gt <th> sex </th> <th> age </th> <th> class </th> <th> mobile </th> &L t;/tr> <tbody id= "Tbody" > <?phpforeach($data  as $k=$v) {?> <tr> <td><?phpEcho $v[' user_id '];? ></td> <td><?phpEcho $v[' username '];? ></td> <td><?phpEcho $v[' pwd '];? ></td> <td><?phpEcho $v[' sex '];? ></td> <td><?phpEcho $v[' Age '];? ></td> <td><?phpEcho $v[' class_name '];? ></td> <td><?phpEcho $v[' phone '];? ></td> </tr> <?php}?> </tbody></table><!--define a hidden--><input of the current page Typ E= "hidden" name= "page" id= "page" value= "1" ><a href= "javascript:void (0);" onclick= "page (' first ')" > Home </a ><a href= "javascript:void (0);" onclick= "page (' prev ')" > Previous </a><a href= "javascript:void (0);" onclick= "page (' Next ')" > next page </a><a href= "javascript:void (0);" onclick= "page (' last ')" > End </a> <script>functionpage (obj) {//get the current page        varCurrent_page = document.getElementById (' page ').value; //Judging page Numbers        if(obj== ' first '){            //Home Page            varZhi = 1; }Else if(obj== ' prev '){            varZhi = parseint (current_page)-1; }Else if(obj== ' Next '){            varZhi = parseint (current_page) +1; }Else{            varZhi = <?phpEcho $zong _page;? >        }        //initiating AJAX Requests//creating AJAX Objects        varAjax =NewXMLHttpRequest (); //Stitching RouteAjax.open ("Get", "fenye.php?page=" +zhi); //initiating a requestAjax.Send (); //Judging ResultsAjax.onreadystatechange=function(){            if(Ajax.readystate==4 && ajax.status==200){                if(ajax.responsetext==0) {alert (' No data '); return false; }Else{                    //have data                    varres = Json.parse (ajax.responsetext); varstr = "; //start my loop.                     for(vari=0;i<res.length;i++){                        //res[i][' username ']str+= "<tr>"; STR+ = "<td>" +res[i][' user_id ']+ "</td>"; STR+ = "<td>" +res[i][' username ']+ "</td>"; STR+ = "<td>" +res[i][' pwd ']+ "</td>"; STR+ = "<td>" +res[i][' sex ']+ "</td>"; STR+ = "<td>" +res[i][' age ']+ "</td>"; STR+ = "<td>" +res[i][' class_name ']+ "</td>"; STR+ = "<td>" +res[i][' phone ']+ "</td>"; STR+ = "</tr>"; }                    //Replace all the contentdocument.getElementById (' tbody '). InnerHTML =str; Document. getElementById (' page '). Value =Zhi; }            }        }    }</script>

fenye.php

<?PHP//connecting to a database$link=Mysqli_connect(' 127.0.0.1 ', ' root ', ' root ', ' mone ');//Set character setsMysqli_query($link, ' Set names UTF8 ');//perform paging operations//receive the current page (if it is empty, we will use the first page, if not empty, we will use the received value)$page=Empty($_get[' page '])? 1:$_get[' Page '];//find the total number of bars$sql= "SELECT * from User";$res=Mysqli_query($link,$sql);$count=mysqli_num_rows($res);//number of bars to display per page$length= 3;//Total Pages$zong _page=Ceil($count/$length);//Offset Amount$limit= ($page-1) *$length;//Enquiry$sql 2= "SELECT * FROM User Limit"$limit,$length";$res 2=Mysqli_query($link,$sql 2); while($a=Mysqli_fetch_assoc($res 2)) {    $data[] =$a;}//determine if the array has any valuesif(Empty($data)){    //if it's empty, I'm outputting a 0 here.    Echo0;}Else{    //If there is a value, echo a JSON data    EchoJson_encode ($data);}?>

Ajax Paging Code

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.