php 에서 페이지 및 SqlHelper 클래스 사용법 실례

본 논문 의 사례 는 phop 의 페이지 와 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'>&lt;</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'>&gt;</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>

사용 하 는 데이터 베 이 스 는 MySQL 5.6 자체 가 가지 고 있 는 World 데이터베이스 입 니 다.
다음은 실행 중인 효과 캡 처 입 니 다:
근 데 코드 에 버그 가 있어 요.마지막 까지 페이지 를 넘 길 때 표시 되 지 않 는 이 유 는 Paging.php 파일 의 41~43 때 문 입 니 다.  좌우 판단 에 문제 가 있다.
오류 코드 는 다음 과 같 습 니 다:

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




더 많은 PHP 관련 내용 에 관심 이 있 는 독자 들 은 본 사이트 의 주 제 를 볼 수 있다.
본 논문 에서 말 한 것 이 여러분 의 PHP 프로 그래 밍 에 도움 이 되 기 를 바 랍 니 다.

좋은 웹페이지 즐겨찾기