 |
|
|
 |
 |
|
ต้อง Save เป็น file php ก่อน แล้วทำการ include เข้าไป ตัวอย่างวิธีใช้อยู่ใน code แล้ว ลองดูเผื่อช่วยได้ฮะ
<?php
/******************* Example : Use Pager.Class.php *************************
require_once("Pager.Class.php"); // Instantiate class
$p = new Pager;
$limit = 100; // Show many results per page?
$start = $p->findStart($limit); // Find the start depending on $_GET['page'] (declared if it's null)
// Find the number of rows returned from a query; Note: Do NOT use a LIMIT clause in this query
$count = mysql_num_rows(mysql_query("SELECT field FROM table"));
// Find the number of pages based on $count and $limit
$pages = $p->findPages($count, $limit);
// Now we use the LIMIT clause to grab a range of rows
$result = mysql_query("SELECT * FROM table LIMIT ".$start.", ".$limit);
// Now get the page list and echo it
$pagelist = $p->pageList($_GET['page'], $pages);
echo $pagelist;
// Or you can use a simple "Previous | Next" listing if you don't want the numeric page listing
//$next_prev = $p->nextPrev($_GET['page'], $pages);
//echo $next_prev;
*********************************************************************************/
class Pager
{
/***********************************************************************************
* int findStart (int limit)
* Returns the start offset based on $_GET['page'] and $limit
***********************************************************************************/
function findStart($limit,$page){
/*
if ((!isset($_GET['page'])) || ($_GET['page'] == "1")){
$start = 0;
$_GET['page'] = 1;
} else {
$start = ($_GET['page']-1) * $limit;
}
*/
if ((!isset($page)) || ($page == "1")){
$start = 0;
$page = 1;
} else {
$start = ($page-1) * $limit;
}
return $start;
}
/***********************************************************************************
* int findPages (int count, int limit)
* Returns the number of pages needed based on a count and a limit
***********************************************************************************/
function findPages($count, $limit){
$pages = (($count % $limit) == 0) ? $count / $limit : floor($count / $limit) + 1;
return $pages;
}
/***********************************************************************************
* string pageList (int curpage, int pages)
* Returns a list of pages in the format of "« < [pages] > »"
***********************************************************************************/
function pageList($Language=1,$curpage, $pages,$count,$limit,$PageListRang=0,$First=1,$Previous=1,$PageListAll=1,$Next=1,$Last=1,
$para1="",$paraval1="", $para2="",$paraval2="", $para3="",$paraval3="" ,$para4="",$paraval4="" , $para5="",$paraval5="" ){
if ($para1 <> "" && $paraval1 <> "" ) {
$paraext = "&" . $para1 . "=" . $paraval1;
} else { $paraext = ""; }
$paraext2 = "";
if ($para2 <> "" && $paraval2 <> "" ) {
$paraext2 = "&" . $para2 . "=" . $paraval2;
} else { $paraext2 = ""; }
$paraext3 = "";
if ($para3 <> "" && $paraval3 <> "" ) {
$paraext3 = "&" . $para3 . "=" . $paraval3;
} else { $paraext3 = ""; }
$paraext4 = "";
if ($para4 <> "" && $paraval4 <> "" ) {
$paraext4 = "&" . $para4 . "=" . $paraval4;
} else { $paraext4 = ""; }
$paraext5 = "";
if ($para5 <> "" && $paraval5 <> "" ) {
$paraext5 = "&" . $para5 . "=" . $paraval5;
} else { $paraext5 = ""; }
if ($Language==1) {
$TextHead = "<FONT color='#000033'><B>Page : </B></FONT>";
$TextTotal = " of about " . $count . " total results ";
$TitleFirstPage = "First Page";
$TextFirstPage = "First";
$TitleLastPage = "Last Page";
$TextLastPage = "Last";
$TitlePrevPage = "Previous Page";
$TextPrevPage = "Previous";
$TitleNextPage = "Next Page";
$TextNextPage = "Next";
} else if ($Language==2) {
$TextHead = "¢èÒÇ : ";
$TextTotal = " ¨Ò¡·Ñé§ËÁ´ " . $count . " ¢èÒÇ " ;
$TitleFirstPage = "˹éÒáá";
$TextFirstPage = "˹éÒáá";
$TitleLastPage = "˹éÒÊØ´·éÒÂ";
$TextLastPage = "˹éÒÊØ´·éÒÂ";
$TitlePrevPage = "˹éÒÂé͹¡ÅѺ";
$TextPrevPage = "Âé͹¡ÅѺ";
$TitleNextPage = "˹éÒ¶Ñ´ä»";
$TextNextPage = "¶Ñ´ä»";
}
$page_list = $TextHead;
if ($PageListRang==1) {
if ($curpage == 1) {
if ($count > $limit) {
$page_list .= ($curpage-1)*$limit+1 . "-" . ($curpage * $limit);
}else{
if ($count==0) {
$page_list .= "0-0";
}else{
$page_list .= ($curpage-1)*$limit+1 . "-" . ($count);
}
}
}elseif ($curpage == $pages) {
$page_list .= ($curpage-1)*$limit+1 . "-" . ($count);
}else{
$page_list .= ($curpage-1)*$limit+1 . "-" . ($curpage * $limit);
}
$page_list .= $TextTotal;
}
/* Print the first and previous page links if necessary */
if ($First==1) {
if (($curpage != 1) && ($curpage)) {
$page_list .= " <a href=\"".$_SERVER['PHP_SELF']."?page=1" . $paraext . $paraext2 . $paraext3 . $paraext4 . $paraext5 . "\" title=\"$TitleFirstPage\"><< $TextFirstPage</a> ";
}
}
if ($Previous==1) {
if (($curpage-1) > 0){
$page_list .= "<a href=\"".$_SERVER['PHP_SELF']."?page=".($curpage-1) . $paraext . $paraext2 . $paraext3 . $paraext4 . $paraext5 ."\" title=\"$TitlePrevPage\">< $TextPrevPage $limit</a> ";
}
}
if ($PageListAll==1) {
/* Print the numeric page list; make the current page unlinked and bold */
for ($i=1; $i<=$pages; $i++) {
if ($i == $curpage){
if ($i == 1) {
$page_list .= "<FONT COLOR='#000000'><b>".$i."</b></FONT>";
} else {
$page_list .= "<FONT COLOR='#000000'>/</FONT> <FONT COLOR='#000000'><b>".$i."</b></FONT>";
}
} else {
if ($i == 1) {
$page_list .= "<a href=\"".$_SERVER['PHP_SELF']."?page=".$i . $paraext . $paraext2 . $paraext3 . $paraext4 . $paraext5 ."\" title=\"Page ".$i."\">".$i."</a>";
} else {
$page_list .= "<FONT COLOR='#000000'>/</FONT> <a class='content' href=\"".$_SERVER['PHP_SELF']."?page=".$i . $paraext . $paraext2 . $paraext3 . $paraext4 . $paraext5 ."\" title=\"Page ".$i."\">".$i."</a>";
}
}
$page_list .= " ";
}
}
/* Print the Next and Last page links if necessary */
if ($Next==1) {
if (($curpage+1) <= $pages){
$page_list .= "<a href=\"".$_SERVER['PHP_SELF']."?page=".($curpage+1) . $paraext . $paraext2 . $paraext3 . $paraext4 . $paraext5 . "\" title=\"$TitleNextPage\">$TextNextPage $limit ></a> ";
}
}
if ($Last==1) {
if (($curpage != $pages) && ($pages != 0)) {
$page_list .= " <a href=\"".$_SERVER['PHP_SELF']."?page=".$pages . $paraext . $paraext2 . $paraext3 . $paraext4 . $paraext5 . "\" title=\"$TitleLastPage\"> $TextLastPage>></a>";
}
}
$page_list .= "</td>\n";
return $page_list;
}
/***********************************************************************************
* string nextPrev (int curpage, int pages)
* Returns "Previous | Next" string for individual pagination (it's a word!)
***********************************************************************************/
function nextPrev($curpage, $pages){
$next_prev = "";
if (($curpage-1) <= 0){
$next_prev .= "Previous";
} else {
$next_prev .= "<a href=\"".$_SERVER['PHP_SELF']."?page=".($curpage-1)."\">Previous</a>";
}
$next_prev .= " | ";
if (($curpage+1) > $pages){
$next_prev .= "Next";
}else {
$next_prev .= "<a href=\"".$_SERVER['PHP_SELF']."?page=".($curpage+1)."\">Next</a>";
}
return $next_prev;
}
}
?>

|
 |
 |
 |
 |
Date :
4 เม.ย. 2548 12:29:47 |
By :
jad |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
<?
$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");
$objDB = mysql_select_db("mydatabase");
$strSQL = "SELECT * FROM customer ";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
$Num_Rows = mysql_num_rows($objQuery);
$Per_Page = 2; // Per Page
$Page = $_GET["Page"];
if(!$_GET["Page"])
{
$Page=1;
}
$Prev_Page = $Page-1;
$Next_Page = $Page+1;
$Page_Start = (($Per_Page*$Page)-$Per_Page);
if($Num_Rows<=$Per_Page)
{
$Num_Pages =1;
}
else if(($Num_Rows % $Per_Page)==0)
{
$Num_Pages =($Num_Rows/$Per_Page) ;
}
else
{
$Num_Pages =($Num_Rows/$Per_Page)+1;
$Num_Pages = (int)$Num_Pages;
}
$strSQL .=" order by CustomerID ASC LIMIT $Page_Start , $Per_Page";
$objQuery = mysql_query($strSQL);
?>
<table width="600" border="1">
<tr>
<th width="91"> <div align="center">CustomerID </div></th>
<th width="98"> <div align="center">Name </div></th>
<th width="198"> <div align="center">Email </div></th>
<th width="97"> <div align="center">CountryCode </div></th>
<th width="59"> <div align="center">Budget </div></th>
<th width="71"> <div align="center">Used </div></th>
</tr>
<?
while($objResult = mysql_fetch_array($objQuery))
{
?>
<tr>
<td><div align="center"><?=$objResult["CustomerID"];?></div></td>
<td><?=$objResult["Name"];?></td>
<td><?=$objResult["Email"];?></td>
<td><div align="center"><?=$objResult["CountryCode"];?></div></td>
<td align="right"><?=$objResult["Budget"];?></td>
<td align="right"><?=$objResult["Used"];?></td>
</tr>
<?
}
?>
</table>
<br>
Total <?= $Num_Rows;?> Record : <?=$Num_Pages;?> Page :
<?
if($Prev_Page)
{
echo " <a href='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page'><< Back</a> ";
}
for($i=1; $i<=$Num_Pages; $i++){
if($i != $Page)
{
echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$i'>$i</a> ]";
}
else
{
echo "<b> $i </b>";
}
}
if($Page!=$Num_Pages)
{
echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$Next_Page'>Next>></a> ";
}
mysql_close($objConnect);
?>
|
 |
 |
 |
 |
Date :
2009-04-26 11:15:15 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|