标签:对象 首页 连接数据库 连接数 else enc tab asc fir
show.php
<meta charset="utf-8"> <?php //连接数据库 $link = mysqli_connect(‘127.0.0.1‘,‘root‘,‘root‘,‘mone‘); //设置字符集 mysqli_query($link,‘set names utf8‘); //进行分页的操作 //接收当前页(如果是空的,我们就使用第一页,如果不是空的,我们就用接收到的值) $page = empty($_GET[‘page‘])?1:$_GET[‘page‘]; //求出总条数 $sql = "select * from user"; $res = mysqli_query($link,$sql); $count = mysqli_num_rows($res); //每页显示的条数 $length = 3; //总页数 $zong_page = ceil($count/$length); //偏移量 $limit = ($page-1)*$length; //查询 $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>姓名</th> <th>密码</th> <th>性别</th> <th>年龄</th> <th>班级</th> <th>手机</th> </tr> <tbody id="tbody"> <?php foreach($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> <!--定义一个当前页的hidden--> <input type="hidden" name="page" id="page" value="1"> <a href="javascript:void(0);" onclick="page(‘first‘)">首页</a> <a href="javascript:void(0);" onclick="page(‘prev‘)">上一页</a> <a href="javascript:void(0);" onclick="page(‘next‘)">下一页</a> <a href="javascript:void(0);" onclick="page(‘last‘)">尾页</a> <script> function page(obj){ //获取一下当前页 var current_page = document.getElementById(‘page‘).value; //判断页码 if(obj==‘first‘){ //首页 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;?> } //发起ajax的请求 //创建ajax对象 var ajax = new XMLHttpRequest(); //拼接路由 ajax.open("get","fenye.php?page="+zhi); //发起请求 ajax.send(); //判断结果 ajax.onreadystatechange=function(){ if(ajax.readyState==4 && ajax.status==200){ if(ajax.responseText==0){ alert(‘无数据‘); return false; }else{ //有数据 var res = JSON.parse(ajax.responseText); var str = ‘‘; //开始我的循环 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>"; } //替换一下所有的内容 document.getElementById(‘tbody‘).innerHTML = str; document.getElementById(‘page‘).value = zhi; } } } } </script>
fenye.php
<?php //连接数据库 $link = mysqli_connect(‘127.0.0.1‘,‘root‘,‘root‘,‘mone‘); //设置字符集 mysqli_query($link,‘set names utf8‘); //进行分页的操作 //接收当前页(如果是空的,我们就使用第一页,如果不是空的,我们就用接收到的值) $page = empty($_GET[‘page‘])?1:$_GET[‘page‘]; //求出总条数 $sql = "select * from user"; $res = mysqli_query($link,$sql); $count = mysqli_num_rows($res); //每页显示的条数 $length = 3; //总页数 $zong_page = ceil($count/$length); //偏移量 $limit = ($page-1)*$length; //查询 $sql2 = "select * from user limit $limit,$length"; $res2 = mysqli_query($link,$sql2); while ($a = mysqli_fetch_assoc($res2)) { $data[] = $a; } //判断一下数组有没有值 if(empty($data)){ //如果是空的,我这里输出一个0 echo 0; }else{ //如果有值,echo一个json数据 echo json_encode($data); } ?>
标签:对象 首页 连接数据库 连接数 else enc tab asc fir
原文地址:https://www.cnblogs.com/hopelooking/p/9215562.html