PHP分页类
我正在寻找一个php分页类,我在过去使用过一个相当简单的类,现在不再支持它 我想知道有没有人有什么建议 当可能有这么多好的软件存在时,建立自己的软件似乎毫无意义。你试过了吗?用法示例。出于以下原因,我建议PHP分页类,php,mysql,class,pagination,pager,Php,Mysql,Class,Pagination,Pager,我正在寻找一个php分页类,我在过去使用过一个相当简单的类,现在不再支持它 我想知道有没有人有什么建议 当可能有这么多好的软件存在时,建立自己的软件似乎毫无意义。你试过了吗?用法示例。出于以下原因,我建议 它是松散耦合的,不需要整个库 ZF社区比PEAR社区更大,正在积极地对代码进行安全审计,并发布维护版本 它使用适配器模式分离数据源,文档中有许多前端UI模式实现的示例 经过更多的搜索,我决定在使用框架版本之前,我应该完全理解分页器中涉及的内容。所以我自己造了一个。谢谢你的建议 您可以尝试以下
- 它是松散耦合的,不需要整个库
- ZF社区比PEAR社区更大,正在积极地对代码进行安全审计,并发布维护版本
- 它使用适配器模式分离数据源,文档中有许多前端UI模式实现的示例
<div class='first-back'><a href='/xampp/web_development/new_study_2014/imagebox2016/app/public/test.php?pageno=1'>First</a> <a href='/xampp/web_development/new_study_2014/imagebox2016/app/public/test.php?pageno=3'>Back</a></div>
<div class='page-count'>(Page 4 of 6)</div>
<div class='next-last'><a href='/xampp/web_development/new_study_2014/imagebox2016/app/public/test.php?pageno=5'>Next</a> <a href='/xampp/web_development/new_study_2014/imagebox2016/app/public/test.php?pageno=6'>Last</a></div>
(第4页,共6页)
您的SQL SELECT语句查询很可能会生成1000条记录。但在一页上显示所有结果并不是一个好主意。因此,我们可以根据需要将该结果划分为多个页面,作为分页类
使用分页类对数据进行分页非常容易
分页类有助于生成分页
如何使用分页类
有关更多信息,请访问此链接
|
公共函数生成分页()
{
$total=0;
$query=“从下载中选择COUNT(downloads.dn\u id),其中downloads.dn\u type='audios';
$stmt=$this->conn->prepare($query);
$stmt->execute();
$total=$stmt->fetchColumn();
//回显“行计数=”。$total;
//每页要列出多少项
$limit=11;
//有多少页
$pages=ceil($total/$limit);
//我们现在在哪一页?
$page=min($pages,filter\u input(input\u GET,'page',filter\u VALIDATE\u INT,数组(
“选项”=>数组(
“默认值”=>1,
“最小范围”=>1,
),
)));
//计算查询的偏移量
$offset=($page-1)*$limit;
//要向用户显示的一些信息
$start=$offset+1;
$end=min($offset+$limit),$total);
//“反向”链接
$prevlink=($page>1)“:”«;&lsaquo;”;
//“向前”链接
$nextlink=($page<$pages)?“:”&rsaquo;”;
//显示分页信息
回显“”.$prevlink.“$Page..”共“$Page..”页“$nextlink..””;
//准备页面查询
$query2=”
从下载、地图艺术家、歌曲艺术家中选择*
其中map\u artists.dn\u id=downloads.dn\u id
和song_artists.artist_id=map_artists.artist_id
AND downloads.dn_type='audios'按downloads.dn_id分组
按下载订购。dn_时间描述限制:限制偏移量:偏移量”;
$stmt2=$this->conn->prepare($query2);
$stmt2->bindParam(':limit',$limit,PDO::PARAM_INT);
$stmt2->bindParam(':offset',$offset,PDO::PARAM_INT);
$stmt2->execute();
//我们有结果吗?
如果($stmt2->rowCount()>0){
//定义获取结果的方式
$stmt2->setFetchMode(PDO::FETCH_ASSOC);
$iterator=新的迭代器($stmt2);
//显示结果
foreach($iterator作为$row){
回显“”.$row['dn_title'].-'.$row['artist_name'.]。”;
}
}否则{
echo'无法显示结果。';
}
}
我使用库是因为我不想关心内部进程。@Lizard你能分享你所做的吗?你仍然不能只下载paginator,而必须下载整个fat Zend Framework,只是为了获得paginator。除此之外,它完全是过度设计的。PEAR的寻呼机也与某些数据源无关。
$pagination = new Pagination($dbh);
$pagination->totalRecords('SELECT COUNT(*) FROM `photos` WHERE `user` = :user', array(':user' => $_SESSION['id']));
$pagination->setLimit(12);
$pagination->page();
echo $pagination->firstBack();
echo $pagination->where();
echo $pagination->nextLast();
<div class='first-back'><a href='/xampp/web_development/new_study_2014/imagebox2016/app/public/test.php?pageno=1'>First</a> <a href='/xampp/web_development/new_study_2014/imagebox2016/app/public/test.php?pageno=3'>Back</a></div>
<div class='page-count'>(Page 4 of 6)</div>
<div class='next-last'><a href='/xampp/web_development/new_study_2014/imagebox2016/app/public/test.php?pageno=5'>Next</a> <a href='/xampp/web_development/new_study_2014/imagebox2016/app/public/test.php?pageno=6'>Last</a></div>
<?php
/**
* @package pagination class
* @version 1.0
*/
/*
@class Name: pagination
@Author: Ahmed Mohamed
@Version: 1.0
@Author URI: https://www.fb.com/100002349977660
@Website URI: http://www.utlearn.com
@class page URI: http://utlearn.com/2017/02/15/pagination-class-use-pagination-class
*/
include_once 'libs/config.php';
include_once 'libs/Database.php';
include_once 'libs/Model.php';
include_once 'libs/pagination.php';
if(!empty($_GET["page"]) and is_numeric($_GET["page"])){
$page = htmlspecialchars(strip_tags($_GET["page"]));
} else {
$page = 1;
}
// news = table name / you page URL / current page / true or false for full query
// its false i just use table name
$pag = new pagination("news", URL."?page=", 3, $page, false);
$pagination = $pag->pagination();
$data = $pag->data();
?>
<news>
<?php foreach($data as $news){ ?>
<header><h1><?=$news->title ?></h1> | <span><?=$news->date ?></span></header>
<div>
<?=$news->content ?>
</div>
<?php } ?>
</news>
<?=$pagination ?>
public function make_pagination()
{
$total = 0;
$query = "SELECT COUNT(downloads.dn_id) FROM downloads WHERE downloads.dn_type = 'audios'";
$stmt = $this->conn->prepare($query);
$stmt->execute();
$total = $stmt->fetchColumn();
//echo 'row_count = ' . $total;
// How many items to list per page
$limit = 11;
// How many pages will there be
$pages = ceil($total / $limit);
// What page are we currently on?
$page = min($pages, filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, array(
'options' => array(
'default' => 1,
'min_range' => 1,
),
)));
// Calculate the offset for the query
$offset = ($page - 1) * $limit;
// Some information to display to the user
$start = $offset + 1;
$end = min(($offset + $limit), $total);
// The "back" link
$prevlink = ($page > 1) ? '<a href="?page=1" title="First page">«</a> <a href="?page=' . ($page - 1) . '" title="Previous page">‹</a>' : '<span class="disabled">«</span> <span class="disabled">‹</span>';
// The "forward" link
$nextlink = ($page < $pages) ? '<a href="?page=' . ($page + 1) . '" title="Next page">›</a> <a href="?page=' . $pages . '" title="Last page">»</a>' : '<span class="disabled">›</span> <span class="disabled">»</span>';
// Display the paging information
echo '<div id="paging"><p>'.$prevlink.' Page '.$page.' of '.$pages. ' pages'. $nextlink.' </p></div>';
//prepare the page query
$query2 = "
SELECT * FROM downloads, map_artists, song_artists
WHERE map_artists.dn_id = downloads.dn_id
AND song_artists.artist_id = map_artists.artist_id
AND downloads.dn_type = 'audios' GROUP BY downloads.dn_id
ORDER BY downloads.dn_time DESC LIMIT :limit OFFSET :offset ";
$stmt2 = $this->conn->prepare($query2);
$stmt2->bindParam(':limit', $limit, PDO::PARAM_INT);
$stmt2->bindParam(':offset', $offset, PDO::PARAM_INT);
$stmt2->execute();
// Do we have any results?
if ($stmt2->rowCount() > 0) {
// Define how we want to fetch the results
$stmt2->setFetchMode(PDO::FETCH_ASSOC);
$iterator = new IteratorIterator($stmt2);
// Display the results
foreach ($iterator as $row) {
echo '<p>'. $row['dn_title'].' - '. $row['artist_name'].'</p>';
}
} else {
echo '<p>No results could be displayed.</p>';
}
}