Article From:https://www.cnblogs.com/hopelooking/p/9215562.html

show.php

<meta charset="utf-8">
<?php
//Connection database
$link = mysqli_connect('127.0.0.1','root','root','mone');
//Set the character set
mysqli_query($link,'set names utf8');

//Paging operations/ / receive the current page (if it is empty, we use the first page. If it is not empty, we will use the received value).
$page = empty($_GET['page'])?1:$_GET['page'];
//Find the number of the total
$sql = "select * from user";
$res = mysqli_query($link,$sql);
$count = mysqli_num_rows($res);
//The number of bars displayed on each page
$length = 3;
//PageCount
$zong_page = ceil($count/$length);
//Offset
$limit = ($page-1)*$length;
//query
$sql2 = "select * from user limit $limit,$length";
$res2 = mysqli_query($link,$sql2);
while ($a = mysqli_fetch_assoc($res2)) {
    $data[] = $a;
}
?>

<table>
    <tr>
        <th>ID</th>
        <th>Name < /th>< th> cipher < /th>< th> sex < /th>< th> age &lT; /th>< th> class < /th>< th> cell phone < /th>< /tr>< tBody id= "tbody" ><? PHPforeach($data as $k => $v){ ?>
    <tr>
        <td><?php echo $v['user_id'];?></td>
        <td><?php echo $v['username'];?></td>
        <td><?php echo $v['pwd'];?></td>
        <td><?php echo $v['sex'];?></td>
        <td><?php echo $v['age'];?></td>
        <td><?php echo $v['class_name'];?></td>
        <td><?php echo $v['phone'];?></td>
    </tr>
    <?php } ?>

    </tbody>
</table>

<!--Define a hidden--> for the current page;< input type= "hidden" name= "page" id= "page" value= "1" >< a href= "JavascrIpt:void (0); "onclick=" page ('first') ">"; home page < /a>< a href= "javascript:void (0);" onclick= "page('prev') "> the last page of < /a>< a href= "javascript:void (0);" onclick= "page ('next')" > next page < /a>< a href= "javascript:void (0);" onclick= "page ('last')" > tail < /a>< script>function page(obj){
        //Get the current page
        var current_page = document.getElementById('page').value;

        //Judge the page number
        if(obj=='first'){
            //home page
            var zhi = 1;
        }else if(obj=='prev'){
            var zhi = parseInt(current_page)-1;
        }else if(obj=='next'){
            var zhi = parseInt(current_page)+1;
        }else{
            var zhi = <?php echo $zong_page;?>
        }

        //A request to initiate a Ajax/ / create Ajax objects
        var ajax = new XMLHttpRequest();
        //Stitching routing
        ajax.open("get","fenye.php?page="+zhi);
        //Initiate a request
        ajax.send();
        //Judgment result
        ajax.onreadystatechange=function(){
            if(ajax.readyState==4 && ajax.status==200){
                if(ajax.responseText==0){
                    alert('No data ');
                    return false;
                }else{
                    //Have data
                    var res = JSON.parse(ajax.responseText);
                    var str = '';
                    //Start my cycle
                    for(var i=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 content
                    document.getElementById('tbody').innerHTML = str;
                    document.getElementById('page').value = zhi;
                }
            }
        }
    }
</script>

 

 

fenye.php

 

<?php
//Connection database
$link = mysqli_connect('127.0.0.1','root','root','mone');
//Set the character set
mysqli_query($link,'set names utf8');

//Paging operations/ / receive the current page (if it is empty, we use the first page. If it is not empty, we will use the received value).
$page = empty($_GET['page'])?1:$_GET['page'];
//Find the number of the total
$sql = "select * from user";
$res = mysqli_query($link,$sql);
$count = mysqli_num_rows($res);
//The number of bars displayed on each page
$length = 3;
//PageCount
$zong_page = ceil($count/$length);
//Offset
$limit = ($page-1)*$length;
//query
$sql2 = "select * from user limit $limit,$length";
$res2 = mysqli_query($link,$sql2);
while ($a = mysqli_fetch_assoc($res2)) {
    $data[] = $a;
}

//Judge whether the array has a value
if(empty($data)){
    //If it's empty, I'll output a 0 here.
    echo 0;
}else{
    //If there is a value, echo a JSON data
    echo json_encode($data);
}
?>

 

Link of this Article: Ajax paging code

Leave a Reply

Your email address will not be published. Required fields are marked *