The practical simple PHP page collection includes usage methods

  • 2020-10-07 18:36:02
  • OfStack

Method 1:


<?php
    /*
     Paging class   Used to achieve multiple data paging display 
    version : 1.0
    Date : 2013-10-20
    */

    /*
         Call very convenient, first connect the database, direct transmission of the query sql String, or you can specify the number of data bars to display per page 
         For example, $pages = new Page('SELECT * FROM `zy_common_member`');
         or   $pages = new Page('SELECT * FROM `zy_common_member`', 10);
    */
    class Page{
        private $curPage;
        private $totalPages;// How many pages of data are displayed in total 
        private $dispNum;// The number of data bars per page 
        private $queryStr;// Of the query SQL statements 
        private $limitStr;// After the query statement limit Control statements 

        /*
         The constructor 
        $queryStr  Querying data SQL statements 
        $dispNum   The number of data bars per page 
        */
        public function __construct($queryStr='',$dispNum=10){    
            $result = mysql_query($queryStr);
            $totalNum = mysql_num_rows($result);
            $this->dispNum = $dispNum;
            $this->totalPages = ceil($totalNum / $dispNum);
            $this->queryStr = $queryStr;

            $temp = (isset($_GET["curPage"]) ? $_GET["curPage"] : 1);
            $this->setCurPage($temp);

            $this->showCurPage();
            $this->showFoot();
        }        

        /* Displays the data content of the current page */
        private function showCurPage(){
            $this->limitStr = ' LIMIT '.(($this->curPage - 1)* $this->dispNum).','.$this->dispNum;
            //echo $this->queryStr.$this->limitStr;
            $result = mysql_query($this->queryStr.$this->limitStr);

            if (!$result)
            {            
                if ($this->totalPages > 0)
                {
                    echo ' Query error '.'<br>';
                }
                else
                {
                    echo ' No data '.'<br>';
                }
                return;
            }
            $cols = mysql_num_fields($result);

            echo '<table border="1">';
            echo '<tr>';
            for($i=0; $i<$cols; $i++)
            {
                echo '<th>';
                echo mysql_field_name($result, $i);
                echo '</th>';
            }
            echo '</tr>';

            while($row = mysql_fetch_assoc($result))
            {
                echo '<tr>';
                foreach($row as $key=>$value)
                {
                    echo '<td>';
                    echo $value;
                    echo '</td>';
                }
                echo '</tr>';
            }

            echo '</table>';
        }

        private function setCurPage($curPage){
                if($curPage < 1)
                {
                    $curPage = 1;
                }
                else if($curPage > $this->totalPages)
                {
                    $curPage = $this->totalPages;
                }
                    $this->curPage = $curPage;
        }

        /*
         Displays the information for the paging footer  
         As on the home page 1 Under the page, 1 Page, back page, etc 
        */
        private function showFoot(){
            echo '<a href="?curPage=1"> Home page </a>';
            echo '<a href="?curPage='.($this->curPage - 1).'"> on 1 page </a>';
            echo '<a href="?curPage='.($this->curPage + 1).'"> Under the 1 page </a>';
            echo '<a href="?curPage='.$this->totalPages.'"> back </a>';
        }

  }

?>


Method 2:


<?php 
class mysqlPager{ 
var $pagePerNum=5;// The number of items per page is displayed  
var $pagePerGroup=5;// The number of pages in each page group  
var $curPage=0;// The current page, Defualt  The first 1 page  
var $totalPage=0;// Total number of pages  
var $totalNum=0;// Total number of data items  
var $curPageGroup=0;// Current page group  
var $curPageUrl="";// Currently using paging  URL 
var $customStyle="";// Custom style  
var $pageQuerySql=""; 
function mysqlPager(){// The constructor  PHP4 
} 
/** 
*  Initializes all variables  
*/ 
function InitAllVar($totalNum,$pagePerGroup,$curPageUrl,$curPage=1,$curPageGroup=1) 
{ 
$this->totalNum=$totalNum; 
$this->pagePerGroup=$pagePerGroup; 
$this->curPageUrl=$curPageUrl; 
$this->curPage=$curPage; 
$this->curPageGroup=$curPageGroup; 
} 
/** 
*  Sets the current page variable  
* 
* @param  digital  $curPage 
*/ 
function setCurPage($curPage) 
{ 
$this->curPage=$curPage; 
} 
/** 
*  Sets the current paging group variable  
* 
* @param mixed $curPageGroup 
*/ 
function setCurPageGroup($curPageGroup) 
{ 
$this->curPageGroup=$curPageGroup; 
} 
/** 
*  Sets the currently used distribution class URL 
* $curPageUrl string 
*/ 
function setCurPageUrl($curPageUrl) 
{ 
$this->curPageUrl=$curPageUrl; 
} 
/** 
*  Get all the  
* 
* @param  digital  $totalNum 
* @param  digital  $curPage 
* @return float 
*/ 
function getTotalPage($totalNum,$curPage=0) 
{ 
return $this->totalPage=ceil($totalNum/$this->pagePerNum); 
} 
/** 
*  Set the user custom style  
* 
* @param mixed $customStyle 
*/ 
function setCustomStyle($customStyle) 
{ 
$this->customStyle=$customStyle; 
} 
/** 
*  Sets a user-defined style to return a string  
* 
* 
* @param mixed $pagerString 
*/ 
function setCustomStyleString($pagerString) 
{ 
return $styleString="<span class=".$customStyle.">".$pagerString."</span>"; 
} 
/** 
*  Output navigation page information   You can do without parameters, but before you use them 1 Make sure to set the corresponding variable  
* 
* @param mixed $curPageGroup 
* @param mixed $curPage 
* @param mixed $curPageUrl 
*/ 
function showNavPager($curPageGroup=0,$curPage=0,$curPageUrl=0) 
{ 
if($curPageGroup) 
{ 
$this->curPageGroup=$curPageGroup; 
} 
if($curPage) 
{ 
$this->curPage=$curPage; 
} 
if($curPageUrl) 
{ 
$this->curPageUrl=$curPageUrl; 
} 
$rtnString=""; 
// Determines whether a variable is initialized  
if($this->curPageGroup && $this->curPageUrl && $this->totalNum && $this->curPage) 
{ 
$this->totalPage=$this->getTotalPage($this->totalNum); 
if($this->curPage==1) 
$this->curPage=($this->curPageGroup-1)*$this->pagePerGroup+1; 
if($this->curPageGroup!=1) 
{ 
$prePageGroup=$this->curPageGroup-1; 
$rtnString.="<a href=".$this->curPageUrl."?cpg=$prePageGroup >".$this->setCustomStyleString("<<")."</a> "; 
} 
for($i=1;$i<=$this->pagePerGroup;$i++) 
{ 
$curPageNum=($this->curPageGroup-1)*$this->pagePerGroup+$i; 
if($curPageNum<=$this->totalPage){ 
if($curPageNum==$this->curPage) 
{ 
$rtnString.=" ".$this->setCustomStyleString($curPageNum); 
}else 
{ 
$rtnString.=" <a href=$this->curPageUrl?cpg={$this->curPageGroup}&cp=$curPageNum >"; 
$rtnString.=$this->setCustomStyleString($curPageNum)."</a>"; 
} 
} 
} 
if($this->curPageGroup<ceil($this->totalPage/$this->pagePerGroup)-1) 
{ 
$nextPageGroup=$this->curPageGroup+1; 
$rtnString.=" <a href=$this->curPageUrl?cpg=$nextPageGroup >".$this->setCustomStyleString(">>")."</a>"; 
} 
$this->pageQuerySql=" limit ".(($this->curPage-1)*$this->pagePerNum).",".$this->pagePerNum; 
} 
else 
{ 
$rtnString=" Error: variable not initialized! "; 
} 
return $rtnString; 
} 
/** 
*  Get the complete query MYSQL the Sql statements  
* 
* @param mixed $sql 
*/ 
function getQuerySqlStr($sql) 
{ 
$allsql=$sql.$this->pageQuerySql; 
return $allsql; 
} 
/** 
*  Sets the number of data items per page  
* 
* @param INT $num 
*/ 
function setPagePerNum($num) 
{ 
$this->pagePerNum=$num; 
} 
} 
?>

Usage:
$curPage=$_GET['cp'];
$curPageGroup=$_GET['cpg']
if($curPage=="")
$curPage=1;
if($curPageGroup=="")
$curPageGroup=1;
// All start from 1, before the incoming data to verify, prevent injection
/ /...
$pager=new MysqlPager();
$pager- > initAllVar(...)
$pager- > showNavPager();
// SQL can be any output
$sql="select id form dbname ";
$querysql=$pager- > getQuerySqlStr($sql)
// In the future, query the database with $querysql to get the corresponding result set


Method 3:

PHP paging function:


< ?  
// To avoid errors caused by repeated inclusion of files, 
 The conditions for judging the existence of the function are added:   
if(!function_exists(pageft)){  
// Define a function pageft(),3 The meanings of the parameters are as follows:   
//$totle : Total amount of information;   
//$displaypg : The number of information displayed per page, which is set as the default 20 ;   
//$url : Links in paging navigation, except for adding different query information 
 " page "The rest is all about this URL The same.   
// The default value should be set to this page URL (i.e. $_SERVER["REQUEST_URI"] ) 
 , but the right side of the default value can only be constant, so the default value is set to an empty string 
 , set to this page inside the function URL .   
function pageft($totle,$displaypg=20,$url= " ){  
// Define several global variables:   
//$page : Current page number;   
//$firstcount (database) The start item of the query;   
//$pagenav : Page navigation bar code, which is not output inside the function;   
//$_SERVER : Read this page URL " $_SERVER["REQUEST_URI"] "Must.   
global $page,$firstcount,$pagenav,$_SERVER;  
// In order for the function to have external access to the" $displaypg ", 
 Set it as a global variable as well. Pay attention to 1 I'm going to reset the variables 
 The original value is overwritten after it is defined as a global variable, so it is re-assigned here.   
$GLOBALS["displaypg"]=$displaypg;  
if(!$page) $page=1;  
// if $url Use the default, which is null, to assign to this page URL :   
if(!$url){ $url=$_SERVER["REQUEST_URI"];}  
//URL Analysis:   
$parse_urlparse_url=parse_url($url);  
$url_query=$parse_url["query"]; 
// Separate out URL The query string of   
if($url_query){  
// because URL It might contain the page number information, and we're going to take it out, 
 To add new page number information.   
// Regular expressions are used here, please refer to" PHP Is a normal expression in" 
$url_query=ereg_replace( " (^|&)page=$page " , " ",$url_query);  

// Will be processed after URL Replace the original query string URL Query string of:   
$url=str_replace($parse_url["query"],$url_query,$url);  

// in URL After add page Query information, but to be assigned:   
if($url_query) $url.= " &page " ; else $url.= " page " ;  
}else {  
$url.= " ?page " ;  
}
$lastpg=ceil($totle/$displaypg);   
// The last page is also the total number of pages   
$page=min($lastpg,$page);  
$prepg=$page-1;   
// on 1 page   
$nextpg=($page==$lastpg ? 0 : $page+1);   
// Under the 1 page   
$firstcount=($page-1)*$displaypg;  
// Start paging navigation bar code:   
$pagenav= "According to the first  < B> " .($totle?($firstcount+1):0). " 
< /B>-< B> " .min($firstcount+$displaypg,$totle). " 
< /B>  A record, total  $totle  records < BR> " ;  
// If only 1 Page out of the function:   
if($lastpg<=1) return false;  
$pagenav.= "  < a href='$url=1 ' > Home page < /a>  " ;  
if($prepg) $pagenav.= "  < a href='$url=$prepg'>
 Previous page < /a>  " ; else $pagenav.= "   Previous page   " ;  
if($nextpg) $pagenav.= "  < a href='$url=$nextpg'>
 After the page < /a>  " ; else $pagenav.= "   After the page   " ;  
$pagenav.= "  < a href='$url=$lastpg'> back < /a>  " ;  
// Drop down the jump list to loop through all the page Numbers:   
$pagenav.= "To the first  < 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>  Page,  $lastpg  Page" ;  
}  
}  
?> 

The pageft() function is called on paging. It doesn't output anything, but produces several global variables for use: $firstcount, $displaypg, $pagenav.
Here's an example of how to use the PHP paging function:


< ?  
// (Omitted from previous program)   
include( " pageft.php " );
 // Contains" pageft.php "File   
// Get the total number of messages   
$result=mysql_query( " select 
* from mytable " );  
$total=mysql_num_rows($result);  
// call pageft() , displayed per page 10 Pieces of information 
 Use the default 20 , this parameter can be omitted), 
 Use this page URL (default, so omitted).   
pageft($total,10);  
// Now the generated global variables come in handy:   
$result=mysql_query( " select * 
from mytable limit $firstcount,
$displaypg  " );  
while($row=mysql_fetch_array($result)){  
// (Omitted from the list)   
}  
// Output paging navigation bar code:   
echo $pagenav;  
// (Omitted below)   
?> 


Related articles: