class PageLink {
/* ページング表示パラメータ設定*/
private $db_table = "";
private $db_table_field = "";//表示されるデータベース内のフィールド
private $ condition = ""; //クエリ条件
private $sort = "" //ソート条件
private $page_size = 0 //各ページに表示されるレコード数
private $link_num = 0;ページ番号リンク
private $page = 1; //現在のページ番号
private $records = 0; //テーブル内の総レコード数
private $page_count = 0; //総ページ数
private $pagestring = " "; //前後のリンク文字をページングする String
private $linkUrl = ""; //現在のページのパス
private $urlPara = ""; //現在のページの URL パラメーター
private $linkUrlNum = 1;
/*
*カスタムページング
*/
private $linkFormat = "";
/* 取得データ*/
private $page_data = "";//データベースから取得したデータ、
// $page_data は受信した二次元配列です
private $dbHelper;
/* 変数定義部終了 */
/*関数定義(クラスメソッド) begin * /
function __construct() {include_once 'DBHelper/DBHelper.php';//これはデータベース操作クラスです
$this->dbHelper = new DBHelper();
} p>
/*
* ページング情報の設定開始
* @param $db_table テーブル
* @param $db_table_field フィールド
* @param $condition 条件
* @param $ sort ソート
* @param $page_size display 項目数
* @param $link_num 数値リンク数
* @param $url ページパス
* @param $para URL パラメータ
*/
public function set($db_table, $ db_table_field, $condition, $sort, $page_size, $link_num, $url, $para) {
$this->db_table = $db_table;//テーブル名
$this->db_table_field = $db_table_field; array,
$this->condition = $condition; //ソート条件
$this->sort = $sort; //表示するフィールド名
//配列に書き込む* db パラメータ設定終了 */
/* ページングパラメータ設定開始 */
$this->page_size = $page_size;//各ページに表示されるレコード数
$this- >link_num = $link_num;//ページめくりリンク数を表示
$this->linkUrl = $url;
$this->urlPara = $para;
/* ページめくりパラメータの設定 end */
} p>
/* ページング情報の設定end */
/* ページングリンクデータ取得 begin */
public function get() {
$page_data[0] = $this->pagestring ;
$page_data[1] = $this->page_data;
return $page_data;
}
/* ページネーション リンク データを取得するend */
private function set_page() {
if (isset($_REQUEST["page"]) ) {
$this->page = intval($ _REQUEST["page"]);
} else {
$this->page = 1;
}
}
/ * ページ番号処理終了 */
< ;p>/* データベース内のレコード数の取得 begin */
private function get_records() {
$this-> ;records = $this->dbHelper->counts($ this->db_table, $this->condition);
}
/* db 内のレコード数を取得しますend */
/* ページめくりリンク文字列の作成 begin */
private function page_link() {
$checkPage = intval($this->ページ / $this->link_num);
$startPage = 1 ;
$stopPage = 1;
if($checkPage == 0 && $this->page < $this->link_num){ $startPage = 1; } $linkPage = ($this->gt; link_num / 2); if($this->page > $linkPage){
$startPage = $this->page - $linkPage;
}
if(($startPage+$this->link_num)> ;$this->page_count){
$startPage = $this->page_count - $this->link_num + 1;
}
if($ startPage$startPage = 1;
}
$ stopPage = $startPage+($this->link_num - 1);
if($stopPage> $this->page_count){
$stopPage = $this ->ページ数;
}
$countStr = "共".$this->records."條記錄";
$currStr = "".$this->page." /".$this- >page_count."ページ ";
$beginLink = "首ページ< ;/a>";
$preLink = "上の一ページ ";
$nextLink = "下のページ ";
$noPreLink = "上のページ ";
$noNextLink = "次のページ ";
$endLink = "尾ページ ";
if($this->page > ($linkPage + 1) && $this->page_count > $this->link_num){
$currPage .= "1 ";
}
for($i =$startPage;$iif($i == $this->page){
$currPage .= "".$i ." ";
}
else{
$currPage .= "".$i." "; } } if(($this->page_count - $this->page) > $linkPage && $this->page_count > $this->link_num){
$currPage .= "... ".$this->page_count." ";
}
$jumpPage .= " ";
$jumpPage .= " linkUrl
."?page='+document.getElementById('page_text_".$this->linkUrlNum."')。 value+'".$this->urlPara
."'" name='page_submit' value='GO' />";
$this->linkUrlNum++;
if(!empty($this->linkFormat )){
$tempLinkFormat = $this->linkFormat;
$tempLinkFormat = str_replace("總記錄", $countStr, $tempLinkFormat);
$tempLinkFormat = str_replace("頁次", $currStr, $tempLinkFormat);
if($this->page_count > 1){
$tempLinkFormat = str_replace("首頁", $beginLink, $tempLinkFormat);
if($this->page > 1){
$tempLinkFormat = str_replace ("上一ページ", $preLink, $tempLinkFormat);
}
else{
$tempLinkFormat = str_replace("上一ページ", $noPreLink, $tempLinkFormat);
}
if($this->page < ; $this->page_count){
$tempLinkFormat = str_replace("次の一頁", $nextLink, $tempLinkFormat);
}
else{
$tempLinkFormat = str_replace("次の一頁", $noNextLink, $tempLinkFormat );
}
$tempLinkFormat = str_replace("尾頁", $endLink, $tempLinkFormat);
$tempLinkFormat = str_replace("分頁", $currPage, $tempLinkFormat);
$tempLinkFormat = str_replace("跳轉", $ JumpPage, $tempLinkFormat);
}
else{
$tempLinkFormat = str_replace("首頁", "", $tempLinkFormat);
$tempLinkFormat = str_replace("上の一頁", "", $tempLinkFormat);
$tempLinkFormat = str_replace("次の一頁", "", $tempLinkFormat);
$tempLinkFormat = str_replace("尾頁", "", $tempLinkFormat);
$tempLinkFormat = str_replace("分頁", "", $tempLinkFormat) ;
$tempLinkFormat = str_replace("跳轉", "", $tempLinkFormat);
}
}
$this->pagestring.=$countStr." ".$currStr;
if($this->ページ数> 1){
$this->pagestring.=$beginLink;
if($this->page> 1){
$this->pagestring.=$preLink;
}
$this->pagestring .=$currPage;
if($this->page < $this->page_count){
$this->pagestring.=$nextLink;
}
$this->pagestring.=$endLink. $jumpPage;
}
}
/* ページめくりリンク文字列の作成 end */
/* データの取得 begin */
private function fetch_data () {
if ($this->records) {
$limit = ($this->page - 1) * $this->page_size . ",$this->page_size";
$this - >page_data = $this->dbHelper->fetch($this->db_table, $this->condition, $this->sort, $limit);
}
} /* データ取得 end */
/* ページング作成 begin */
public function create_page() {
$this-> ; set_page();
$this->get_records();
$this->page_count = ceil($this->records / $this->page_size);
$this->page_link();
$this->fetch_data();
}
/* ページネーションの作成 end */
function __destruct() {
< ;p>}
/* 関数定義(クラスメソッド) end */
}
/*
メソッド呼び出し
include_once 'PageLink.php';
$pageLink = new PageLink() ; //インスタントオブジェクト
$pageLink->set("テーブル", "*(またはフィールド)", "条件", "ソート", "データ項目数", "ページごとのリンク数", "page( list.php)", "その他のパラメータ (?page=1 (&id=1&name=test) を除く)");//パラメータを渡します
$pageLink->create_page();// ページングを作成します
$list = $ pageLink- >get();//データを取得します
echo $list[0];//ページリンク
print_r($list[1]); //データを出力するか、必要に応じてループアウトします
*/ < ;/p>
コードをコピー