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>
사용 하 는 데이터 베 이 스 는 MySQL 5.6 자체 가 가지 고 있 는 World 데이터베이스 입 니 다.다음은 실행 중인 효과 캡 처 입 니 다:
근 데 코드 에 버그 가 있어 요.마지막 까지 페이지 를 넘 길 때 표시 되 지 않 는 이 유 는 Paging.php 파일 의 41~43 때 문 입 니 다. 좌우 판단 에 문제 가 있다.
오류 코드 는 다음 과 같 습 니 다:
if($nowPage>$this->pageCount || $nowPage<=0){
return false;
}
더 많은 PHP 관련 내용 에 관심 이 있 는 독자 들 은 본 사이트 의 주 제 를 볼 수 있다.
본 논문 에서 말 한 것 이 여러분 의 PHP 프로 그래 밍 에 도움 이 되 기 를 바 랍 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Laravel - 변환된 유효성 검사 규칙으로 API 요청 제공동적 콘텐츠를 위해 API를 통해 Laravel CMS에 연결하는 모바일 앱(또는 웹사이트) 구축을 고려하십시오. 이제 앱은 CMS에서 번역된 콘텐츠를 받을 것으로 예상되는 다국어 앱이 될 수 있습니다. 일반적으로 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.