成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久

您的位置:首頁技術(shù)文章
文章詳情頁

PHP完美結(jié)合MYSQL數(shù)據(jù)庫記錄分頁顯示

瀏覽:117日期:2024-01-15 16:29:20

在php網(wǎng)絡數(shù)據(jù)庫編程時,不可避免的要考慮到數(shù)據(jù)庫記錄結(jié)果的顯示問題,為了呈現(xiàn)美觀頁面并加快頁面的載入速度,就需要對數(shù)據(jù)庫記錄進行分頁顯示。

現(xiàn)把與php完美結(jié)合的mysql數(shù)據(jù)庫記錄的分頁顯示實例拿出來與大家共享。

mysql數(shù)據(jù)庫為xinxiku,數(shù)據(jù)表為joke。其定義sql語句如下:

create table joke (

id int(5) not null auto_increment,

biaoti varchar(40) not null,

neirong text not null,

primary key (id)

);字段說明:

id:記錄號,自動遞增且為主鍵

biaoti:標題

neirong :內(nèi)容<html>

<head>

<title>分頁顯示的實現(xiàn)方法</title>

<meta http-equiv=”content-type” content=”text/html; charset=gb2312″>

<script language=”javascript”>

/* 定義一彈出窗口,來顯示具體內(nèi)容*/

function popwin(url)

{

window.open(url,”',”left=340, top=190, height=280, width=400, resizable=yes, scrollbars=yes, status=no, toolbar=no, menubar=no, location=no”);

}

</script>

</head>

<body leftmargin=0 topmargin=0 rightmargin=0 >

<?php

//連接數(shù)據(jù)庫

$db=mysql_connect(”localhost”,”root”,”');

mysql_select_db(”xinxiku”,$db);

//設定每一頁顯示的記錄數(shù)

$pagesize=15;

//取得記錄總數(shù),計算總頁數(shù)用

$res=mysql_query(”select count(*) from joke ” ,$db);

$myrow = mysql_fetch_array($res);

$numrows=$myrow[0];

//計算總頁數(shù)

$pages=intval($numrows/$pagesize);

if ($numrows%$pagesize)

$pages++;

//判斷頁數(shù)設置與否,如無則定義為首頁

if (!isset($page))

$page=1;

//判斷轉(zhuǎn)到頁數(shù)

if (isset($ys))

if ($ys>$pages)

$page=$pages;

else

$page=$ys;

//計算記錄偏移量

$offset=$pagesize*($page-1);

//取記錄

$res=mysql_query(”select id,biaoti from joke order by id desc limit $offset,$pagesize” ,$db);

//循環(huán)顯示記錄

if ($myrow = mysql_fetch_array($res))

{

$i=0;

?>

<table width=”101%” border=”0″ cellspacing=”0″ cellpadding=”0″>

<tr>

<td width=”5%” bgcolor=”#e1e9fb”></td>

<td width=”95%” bgcolor=”#e1e9fb”><font color=”#ff6666″ size=”2″>內(nèi)容</font></td>

</tr>

<?php

do {

$i++;

?>

<tr>

<td width=”5%” bgcolor=”#e6f2ff”><?php echo $i;?></td>

<td width=”95%” bgcolor=”#e6f2ff”><font size=”2″>

<a href=”javascript:popwin(’jokenr.php?id=<?php echo $myrow[0];?>’)” ><?php echo $myrow[1];?></a></font></td>

</tr>

<?php

}

while ($myrow = mysql_fetch_array($res));

echo “</table>” ;

}

//顯示總頁數(shù)

echo “<div align=’center’>共有”.$pages.”頁(”.$page.”/”.$pages.”)<br>”;

//顯示分頁數(shù)

for ($i=1;$i<$page;$i++)

echo “<a href=’fy.php?page=”.$i.”‘>第”.$i .”頁</a> “;

echo “第”.$page.”頁 “;

for ($i=$page+1;$i<=$pages;$i++)

echo “<a href=’fy.php?page=”.$i.”‘>第”.$i .”頁</a> “;

echo “<br>”;

//顯示轉(zhuǎn)到頁數(shù)

echo “<form action=’fy.php’ method=’post’> “;

//計算首頁、上一頁、下一頁、尾頁的頁數(shù)值

$first=1;

$prev=$page-1;

$next=$page+1;

$last=$pages;

if ($page>1){echo “<a href=’fy.php?page=”.$first.”‘>首頁</a> “;

echo “<a href=’fy.php?page=”.$prev.”‘>上一頁</a> “;

}

if ($page<$pages)

{

echo “<a href=’fy.php?page=”.$next.”‘>下一頁</a> “;

echo “<a href=’fy.php?page=”.$last.”‘>尾頁</a> “;

}

echo “轉(zhuǎn)到<input type=text name=’ys’ size=’2′ value=”.$page.”>頁”;

echo “<input type=submit name=’submit’ value=’go’>”;

echo “</form>”;

echo “</div>”;

?>

</body>

</html>

上一頁 1 23 下一頁

程序說明:

1、變量說明

變量$page:存放當前要顯示的頁數(shù)。

變量$pages :存放總頁數(shù)。

變量$offset :存放當前頁數(shù)的偏移量。

變量$pagesize:存放每頁中顯示的記錄數(shù)。

2、mysql語句說明

select id,biaoti from joke order by id desc limit $offset,$pagesizelimit子句用來限制select語句返回的行數(shù)。limit中第一個參數(shù)指定要返回的第一行的偏移量offset,第二個參數(shù)指定返回行的最大數(shù)目pagesize。

實現(xiàn)原理:

在程序中傳遞一頁數(shù)變量page,在取記錄時根據(jù)傳遞的頁數(shù)值來計算出初始記錄位置offset,再根據(jù)每頁中要求顯示的記錄數(shù)pagesize取得記錄集。然后顯示之。

程序?qū)崿F(xiàn)功能:

程序提供了兩種頁顯示的方法(假定當前頁為第三頁)。

1、把所有的頁數(shù)都顯示出來,根據(jù)頁數(shù)提供鏈接,當前頁不提供鏈接。這適合于頁數(shù)不是太多的情況。樣式如圖1所示:

/ShowImg.asp?p=/2006-3-29/19430aimagea1.jpg2、提供總頁數(shù),翻頁通過提供的首頁、上一頁、下一頁、尾頁和跳轉(zhuǎn)到來實現(xiàn)。當然,如果當前頁為每一頁,不提供首頁、上一頁的鏈接,當前頁為最后一頁時,不提供下一頁、尾頁的鏈接。

上一頁 1 2 3

標簽: PHP