当前位置: 移动技术网 > IT编程>开发语言>PHP > php中分页及SqlHelper类用法实例

php中分页及SqlHelper类用法实例

2017年12月12日  | 移动技术网IT编程  | 我要评论

迅雷电影下载网站,女孩子生日送什么好,王近山王少峰

本文实例讲述了php中分页及sqlhelper类用法。分享给大家供大家参考,具体如下:

文档目录结构如下:

sqlhelper.php代码如下:

<?php
/**
 * created by jetbrains phpstorm.
 * user: lee
 * date: 13-7-26
 * time: 下午8:30
 * to change this template use file | settings | file templates.
 */
class sqlhelper{
  private $mysqli;
  private static $host="localhost";
  private static $user="root";
  private static $pwd="";
  private static $db="world";
  private $sql=false;
  private $result=false;
  function __construct(){
    $this->mysqli=new mysqli(self::$host,self::$user,self::$pwd,self::$db);
    if($this->mysqli->connect_error){
      die("连接数据库失败! ".$this->mysql->connect_error);
    }
    $this->mysqli->query("set names utf8");
  }
  function execute_dql_all($sql){
    //执行查询语句
    $arr=array();
    $this->result=$this->mysqli->query($sql) or die ($this->mysql->connect_error);
    //将数据转存到$arr数组中
    while($row=mysqli_fetch_array($this->result,mysql_both)){
      $arr[]=$row;
    }
    $this->result->free();
    return $arr;
  }
  function execute_dql_num($sql){
    //执行查询语句
    $arr=array();
    $this->result=$this->mysqli->query($sql) or die ($this->mysql->connect_error);
    //将数据转存到$arr数组中
    while($row=mysqli_fetch_array($this->result,mysqli_num)){
      $arr[]=$row;
    }
    $this->result->free();
    return $arr;
  }
  function execute_dql_assoc($sql){
    //执行查询语句
    $arr=array();
    $this->result=$this->mysqli->query($sql) or die ($this->mysql->connect_error);
    //将数据转存到$arr数组中
    while($row=mysqli_fetch_array($this->result,mysqli_assoc)){
      $arr[]=$row;
    }
    $this->result->free();
    return $arr;
  }
  //查询某表中的记录数
  function execute_dql_counts($table,$id="*"){
    $this->sql="select count($id) from $table";
    $this->result=$this->mysqli->query($this->sql);
    $row=mysqli_fetch_all($this->result);
    $this->result->free();
    return $row[0][0];
  }
  function execute_dml($sql){
    //执行正删改
    $this->result=$this->mysqli->query($sql);
    if(!$this->result){
      return -1;//执行正删改失败
    }else{
      if($this->mysqli->affected_rows>0){
        return 1;//执行正删改成功,影响行数
      }else{
        return 0;//执行正删改成功,但没有影响行数
      }
    }
  }
}

paging.php代码如下:

<?php
/**
 * created by jetbrains phpstorm.
 * user: lee
 * date: 13-7-27
 * time: 下午2:48
 * to change this template use file | settings | file templates.
 */
header("content-type:text/html;charset=utf-8;");
require_once("sqlhelper.php");
class paging {
  private $sqlhelper=false;
  private $pagecount=false;//页数
  private $counts=false;//总记录数
  private $returnarr=false;//分页超链接的分页
  function __construct(){
    $this->sqlhelper=new sqlhelper();
    $this->returnarr=array();
  }
  /*
   * 参数说明
   *
   * $table 分页时对那个表的数据分页
   * $id 辅助查询当前分页的数据表的总记录数
   * $pagesize 每页显示多少条信息记录数
   * $pagingsize 分页栏每次循环显示出来的个数
   * $nowpage 当前是第几页,默认第一页
   * $href 分页栏的超链接将要往哪里连接
   */
  function paging_prev_next($table,$id="*",$pagesize,$pagingsize,$nowpage=1,$href){
    $this->counts=$this->sqlhelper->execute_dql_counts($table,$id);
    $this->pagecount=ceil($this->counts/$pagesize);
    $this->returnarr["count"]=$this->counts;
    $this->returnarr["start"]=($nowpage-1)*$pagesize;
    $this->returnarr["limit"]=$pagesize;
    if($nowpage>$this->pagecount || $nowpage<=0){
      return false;
    }
    $t=(ceil($nowpage/$pagingsize)-1)*$pagingsize+1;
    $pre=$nowpage-$pagingsize;
    $nex=$nowpage+$pagingsize;
    echo "
      <span class='paging-list-a paging-list-a-withbg'>{$nowpage}/{$this->pagecount}</span>
      <a href='{$href}?nowpage={$pre}' class='paging-list-a'><</a>";
    for($i=$t;$i<$t+$pagingsize;$i++){
      if($i*$pagesize>$this->pagecount*$pagesize){
        break;
      }else{
        if($nowpage==$i){
          echo "
          <a href='{$href}?nowpage={$i}' class='paging-list-a paging-list-a-withbg'>{$i}</a>";
        }else{
          echo "
          <a href='{$href}?nowpage={$i}' class='paging-list-a'>{$i}</a>";
        }
      }
    }
    echo "
      <a href='{$href}?nowpage={$nex}' class='paging-list-a'>></a>";
    return $this->returnarr;
  }
}

paging-list-link.css代码如下:

/**
 * created by jetbrains phpstorm.
 * user: lee
 * date: 13-7-27
 * time: 下午5:56
 * to change this template use file | settings | file templates.
 */
.paging-list-a{
  border:1px solid #b5b5af;
  background-color:#efebed;
  font-family: 'meiryo ui';
  font-size: 16px;
  font-weight: 600;
  padding: 0px 8px 0px 8px;
  /*cursor: pointer;*/
  text-decoration: none;
  color: #292927;
}
.paging-list-a-withbg{
  background-color: #1d92e2;
  color: white;
}

usepaging.php代码如下:

<!doctype html>
<html>
<head>
  <title></title>
  <link rel="stylesheet" type="text/css" href="paging-list-link.css">
</head>
<body>
<?php
header("content-type:text/html;charset=utf-8;");
require_once 'paging.php';
$paging=new paging();
//参数说明
/*
 * $table 分页时对那个表的数据分页
 * $id 辅助查询当前分页的数据表的总记录数
 * $pagesize 每页显示多少条信息记录数
 * $pagingsize 分页栏每次循环显示出来的个数
 * $nowpage 当前是第几页,默认第一页
 * $href 分页栏的超链接将要往哪里连接,当前页链接地址
 */
//控制起始页为
$nowpage=1;
if(isset($_get["nowpage"])){
  $nowpage=$_get["nowpage"];
}
//定义分页所需参数
$meiyexiansi=10;
$meiyelianjieshu=10;
$receivearr=array();
$receivearr=$paging->paging_prev_next("city","id",$meiyexiansi,$meiyelianjieshu,$nowpage,"usepaging.php");
//容错判断
if(!$receivearr){
  return;
}
//查询每页需要显示的数据,大小限制存在 $receivearr 数组中
$sqlhelper=new sqlhelper();
$result=$sqlhelper->execute_dql_num("select * from city limit ".$receivearr['start'].",".$receivearr['limit']."");
echo "<pre>";
print_r($result);
echo "</pre>";
?>
</body>
</html>

所使用的数据库为 mysql5.6 所自带的 world 数据库

下面是运行的效果截图:

不过代码还有个 bug 。就是翻页到最后的时候会出现显示不了,原因在于 paging.php 文件的 41~43  行左右判断有问题。

错误代码如下:

if($nowpage>$this->pagecount || $nowpage<=0){
  return false;
}

更多关于php相关内容感兴趣的读者可查看本站专题:《php+mysql数据库操作入门教程》、《php+mysqli数据库程序设计技巧总结》、《php面向对象程序设计入门教程》、《php数组(array)操作技巧大全》、《php字符串(string)用法总结》、《php网络编程技巧总结》及《php常见数据库操作技巧汇总

希望本文所述对大家php程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网