【原创】PHP 表格 分页 显示

很多人说 表格加载很慢 要做数据优化 我觉得 哪怕是几万条数据 在table里面显示出来 用limit 也一样

反正记住一点 表格分页显示 肯定是需要 limit的


好 我们开始写代码
html部分就省略了。。。
php代码开始

//分页开始
//开始搜索分页
if($_GET['name']!=''){
    //对用户名进行所有所需要的拼接(查询条件根据自己的情况拼接)
    $name = $_GET['name'];
    $where ="WHERE name LIKE '%{$name}%'";
    $url ="&name={$name}";
}

//每页显示数
$num = 6;//这边也可以自己get提交用一个if判断一下
//拿到总条数
$sql="SELECT count(id) total FROM ".PRE."user";
$result = mysqli_query($link,$sql);
if($result && mysqli_num_rows($result)>0){
  $row = mysqli_fetch_assoc($result);  
}
//得到总条数
$total = $row['total'];
//echo $total;
//得到总页数
$amount= ceil($total/$num);
//接受页码
$page = (int)$_GET['page'];
//判断页码范围
if($page <=0){
    $page =1;
}
if($page>=$amount){
    $page=$amount;
}
//偏移量
$offset = ($page -1)*$num;
$sql ="SELECT * FROM ".PRE."user LIMIT {$offset},{$num} ";
$result =mysqli_query($link,$sql);
if($result && mysqli_num_rows($result)>0){
     $userlist = array();
     while($row = mysqli_fetch_assoc($result)){
          $userlist[]=$row;
     }
}
    //下一页
    $next = $page +1;
    //上一页
    $prev = $page -1;
?>

第几页 一共多少页的 直接贴上 一段代码

<td align="left" valign="top" class="fenye"> echo $total?>条数据  echo $page?>/ echo $amount ?> 页  <a href="index.php?page=1" target="mainFrame" onFocus="this.blur()">首页a>  <a href="index.php?page=" target="mainFrame" onFocus="this.blur()">上一页a>  <a href="index.php?page=" target="mainFrame" onFocus="this.blur()">下一页a>  <a href="index.php?page=" target="mainFrame" onFocus="this.blur()">尾页a>td>

你可能感兴趣的:(php数据)