php列表分頁
Ⅰ php怎麼製作分頁
方法/步驟
首先要了解下分頁的原理即
SELECT * FROM table …… limit 開始位置 , 操作條數
開始位置從0開始
SELECT * FROM table …… limit 0 , 20
取最前面20條
SELECT * FROM table …… limit 10 , 20
11條到20條
其次分頁要用的公式
得到公式
(當前頁數 - 1 )X 每頁條數 , 每頁條數
Select * from table limit ($Page- 1) * $PageSize, $PageSize
還要了解parse_url()解析URL函數
parse_url() 是講URL解析成有固定鍵值的數組的函數
$ua=parse_url("http://username:password@hostname/path?arg=value#anchor");
print_r($ua);
結果:
Array
(
[scheme] => http
[host] => hostname
[user] => username
[pass] => password
[path] => /path
[query] => arg=value
[fragment] => anchor
)
創建資料庫bbs和表test有
CREATE TABLE `test` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(50) character set gb2312 NOT NULL,
`sex` varchar(2) character set gb2312 NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;
然後插入十幾條測試數據即可
寫mysql資料庫連接代碼保存conn.php文件里
代碼如下
<?php
$conn = @ mysql_connect("localhost", "root", "") or die("資料庫鏈接錯誤");
mysql_select_db("bbs", $conn);
mysql_query("set names 'GBK'"); //使用GBK中文編碼;
function htmtocode($content) {
$content = str_replace("\n", "<br>", str_replace(" ", " ", $content));
return $content;
}
//$content=str_replace("'","'",$content);
//htmlspecialchars();
?>
寫分頁函數page.php
代碼如下
<?php
function _PAGEFT($totle, $displaypg = 20, $url = '') {
global $page, $firstcount, $pagenav, $_SERVER;
$GLOBALS["displaypg"] = $displaypg;
if (!$page)
$page = 1;
if (!$url) {
$url = $_SERVER["REQUEST_URI"];
}
//URL分析:
$parse_url = parse_url($url);
$url_query = $parse_url["query"]; //單獨取出URL的查詢字串
if ($url_query) {
$url_query = ereg_replace("(^|&)page=$page", "", $url_query);
$url = str_replace($parse_url["query"], $url_query, $url);
if ($url_query)
$url .= "&page";
else
$url .= "page";
} else {
$url .= "?page";
}
$lastpg = ceil($totle / $displaypg); //最後頁,也是總頁數
$page = min($lastpg, $page);
$prepg = $page -1; //上一頁
$nextpg = ($page == $lastpg ? 0 : $page +1); //下一頁
$firstcount = ($page -1) * $displaypg;
//開始分頁導航條代碼:
$pagenav = "顯示第 <B>" . ($totle ? ($firstcount +1) : 0) . "</B>-<B>" . min($firstcount + $displaypg, $totle) . "</B> 條記錄,共 $totle 條記錄";
//如果只有一頁則跳出函數:
if ($lastpg <= 1)
return false;
$pagenav .= " <a href='$url=1'>首頁</a> ";
if ($prepg)
$pagenav .= " <a href='$url=$prepg'>前頁</a> ";
else
$pagenav .= " 前頁 ";
if ($nextpg)
$pagenav .= " <a href='$url=$nextpg'>後頁</a> ";
else
$pagenav .= " 後頁 ";
$pagenav .= " <a href='$url=$lastpg'>尾頁</a> ";
//下拉跳轉列表,循環列出所有頁碼:
$pagenav .= " 到第 <select name='topage' size='1' onchange='window.location=\"$url=\"+this.value'>\n";
for ($i = 1; $i <= $lastpg; $i++) {
if ($i == $page)
$pagenav .= "<option value='$i' selected>$i</option>\n";
else
$pagenav .= "<option value='$i'>$i</option>\n";
}
$pagenav .= "</select> 頁,共 $lastpg 頁";
}
include("conn.php");
$result=mysql_query("SELECT * FROM `test`");
$total=mysql_num_rows($result);
//調用pageft(),每頁顯示10條信息(使用默認的20時,可以省略此參數),使用本頁URL(默認,所以省略掉).
_PAGEFT($total,5);
echo $pagenav;
$result=mysql_query("SELECT * FROM `test` limit $firstcount,$displaypg ");
while($row=mysql_fetch_array($result)){
echo "<hr><b>".$row[name]." | ".$row[sex];
}
?>
調用數據和分頁list.php
<?php
/**
* 愛微網 www.iiwnet.com/php/
*/
include("conn.php");
$pagesize=5;
$url=$_SERVER["REQUEST_URI"];
$url=parse_url($url);
$url=$url[path];
$numq=mysql_query("SELECT * FROM `test`");
$num = mysql_num_rows($numq);
if($_GET[page]){
$pageval=$_GET[page];
$page=($pageval-1)*$pagesize;
$page.=',';
}
if($num > $pagesize){
if($pageval<=1)$pageval=1;
echo "共 $num 條".
" <a href=$url?page=".($pageval-1).">上一頁</a> <a href=$url?page=".($pageval+1).">下一頁</a>";
}
echo $SQL="SELECT * FROM `test` limit $page $pagesize ";
$query=mysql_query($SQL);
while($row=mysql_fetch_array($query)){
echo "<hr><b>".$row[name]." | ".$row[sex];
}
?>
注意事項
注意分頁公式寫法你只要記住即可;
(當前頁數 - 1 )X 每頁條數 , 每頁條數;
Select * from table limit ($Page- 1) * $PageSize, $PageSize;
注意三個代碼文件在同一個目錄下;
主要是最後的list.php調用代碼要細看很有用。
Ⅱ php文章列表分頁問題
給你個分頁類吧,這個好用.也簡單.
//文件名Page.class.php
<?php
class Pager{
/**
*int總頁數
**/
protected $pageTotal;
/**
*int上一頁
**/
protected $previous;
/**
*int下一頁
**/
protected $next;
/**
*int中間頁起始序號
**/
protected $startPage;
/**
*int中間頁終止序號
**/
protected $endPage;
/**
*int記錄總數
**/
protected $recorbTotal;
/**
*int每頁顯示記錄數
**/
protected $pageSize;
/**
*int當前顯示頁
**/
protected $currentPage;
/**
*string基url地址
**/
protected $baseUri;
/**
*@returnstring獲取基url地址
*/
public function getBaseUri(){
return $this->baseUri;
}
/**
*@returnint獲取當前顯示頁
*/
public function getCurrentPage(){
return $this->currentPage;
}
/**
*@returnint獲取每頁顯示記錄數
*/
public function getPageSize(){
return $this->pageSize;
}
/**
*@returnint獲取記錄總數
*/
public function getRecorbTotal(){
return$this->recorbTotal;
}
/**
*@paramstring$baseUri設置基url地址
*/
public function setBaseUri($baseUri){
$this->baseUri=$baseUri;
}
/**
*@paramint$currentPage設置當前顯示頁
*/
public function setCurrentPage($currentPage){
$this->currentPage=$currentPage;
}
/**
*@paramint$pageSize設置每頁顯示記錄數
*/
public function setPageSize($pageSize){
$this->pageSize=$pageSize;
}
/**
*@paramint$recorbTotal設置獲取記錄總數
*/
public function setRecorbTotal($recorbTotal){
$this->recorbTotal=$recorbTotal;
}
/**
*構造函數
**/
public function __construct()
{
$this->pageTotal=0;
$this->previous=0;
$this->next=0;
$this->startPage=0;
$this->endPage=0;
$this->pageSize=20;
$this->currentPage=0;
}
/**
*分頁演算法
**/
private function arithmetic(){
if($this->currentPage<1)
$this->currentPage=1;
$this->pageTotal=floor($this->recorbTotal/$this->pageSize)+($this->recorbTotal%$this->pageSize>0?1:0);
if($this->currentPage>1&&$this->currentPage>$this->pageTotal)
header('location:'.$this->baseUri.'page='.$this->pageTotal);
$this->next=$this->currentPage+1;
$this->previous=$this->currentPage-1;
$this->startPage=($this->currentPage+5)>$this->pageTotal?$this->pageTotal-10:$this->currentPage-5;
$this->endPage=$this->currentPage<5?11:$this->currentPage+5;
if($this->startPage<1)
$this->startPage=1;
if($this->pageTotal<$this->endPage)
$this->endPage=$this->pageTotal;
}
/**
*分頁樣式
**/
protected function pageStyle(){
$result="共".$this->pageTotal."頁";
if($this->currentPage>1)
$result.="<a href=\"".$this->baseUri."page=1\">第1頁</a> <a href=\"".$this->baseUri."page=$this->previous\">前一頁</a>";
else
$result.="第1頁";
for($i=$this->startPage;$i<=$this->endPage;$i++){
if($this->currentPage==$i)
$result.="$i";
else
$result.=" <a href=\"".$this->baseUri."page=$i\">$i</a> ";
}
if($this->currentPage!=$this->pageTotal){
$result.="<a href=\"".$this->baseUri."page=$this->next\">後一頁</a>";
$result.="<a href=\"".$this->baseUri."page=$this->pageTotal\">最後1頁</a>";
}else{
$result.="最後1頁";
}
return $result;
}
/**
*執行分頁
**/
public function execute(){
if($this->baseUri!=""&&$this->recorbTotal==0)
return"";
$this->arithmetic();
return $this->pageStyle();
}
}
?>
//上邊是類
//在HTML中調用方法
//先查出資料庫中的總合
<?php
require("../inc/conn.php");
include_once("../js/Pager.class.php");
$sql="select * from content order by id desc";
$rs = mysql_query($sql);
$pager = new Pager();
$pageno = $_GET["page"];
if ($pageno == "")
{
$pageno = 1;
}
$pagesize = 10;
$startno = ($pageno-1) * $pagesize;
$recordcount = mysql_num_rows($rs);
$sql = "select * from content order by id desc limit $startno,$pagesize";
$rs = mysql_query($sql);
//$count=mysql_num_rows($rs);
$i = 1;
while($rows = mysql_fetch_assoc($rs))
{
//這里邊是循環體,不用說應該知道吧
}
//這底下的執行分頁
<?php
if(isset($_GET['page']))
{
$pager->setCurrentPage($_GET["page"]);
}
else
{
$pager->setCurrentPage(1);
}
$pager->setPageSize($pagesize);
$pager->setRecorbTotal($recordcount);
$pager->setBaseUri("news.php?");
echo $pager->execute();
?>