The page number on the left of the current page is the latest product, which is listed in ascending order by update time. the page number on the right is the previous product, which is sorted in descending order by update time. If the number of records on the left is less than the value of $ space (page number segment), the value of page number $ start is increased from 1 to the right. If the number of records on the left is more than the value of $ left (the number of pages displayed on the left and right), $ start will start from the number of records on the left minus the value of $ left.
The code is as follows:
/** Turn pages of similar products **/
Class pager
{
Protected $ space;
Protected $ left;
Protected $ DB;
Protected $ pageName;
Public function setSpace ($ num ){
$ This-> space = $ num;
$ This-> left = ceil ($ num-1)/2 );
}
Public function setDB (& $ db ){
$ This-> DB = $ db;
}
Public function setPageName ($ pageName ){
$ This-> pageName = $ pageName;
}
Public function getPages ($ catid, $ exptime ){
$ Fields = array ("'id', 'title '");
$ Left = array (">" => array ("exptime" => $ exptime), "memberid" => gs (_ MEM_PREFIX _. "memberid"), "catid" => $ catid );
$ Right = array ("<" => array ("exptime" => $ exptime), "memberid" => gs (_ MEM_PREFIX _. "memberid"), "catid" => $ catid );
$ LeftCount = $ this-> DB-> getCount ($ left );
If ($ leftCount <= $ this-> left ){
$ Star = 1;
$ LeftLimit = "LIMIT". $ leftCount;
$ RightLimit = "LIMIT". ($ this-> space-$ leftCount );
}
Else {
$ Start = $ leftCount-$ this-> left;
$ LeftLimit = "LIMIT". $ this-> left;
$ RightLimit = $ leftLimit;
}
$ List1 = $ this-> DB-> findAll ($ left, array ("exptime" => "ASC"), $ leftLimit, $ fields );
$ List2 = $ this-> DB-> findAll ($ right, array ("exptime" => "DESC"), $ rightLimit, $ fields );
/** Previous page link **/
$ C = count ($ list1 );
If ($ c> 1 ){
$ Url = $ this-> pageName. "-". $ list1 [$ c] ['id']. ". html ";
$ Pages = "previous page
";
} Elseif ($ c = 1 ){
$ Url = $ this-> pageName. "-". $ list1 [0] ['id']. ". html ";
$ Pages = "previous page
";
} Else {
$ Pages = "";
}
/** Content on the left of the current page **/
Foreach ($ list1 as $ item ){
$ Url = $ this-> pageName. "-". $ item ['id']. ". html ";
$ Pages. ="
- {$ Start}
";
$ Start ++;
}
$ Pages. ="
- {$ LeftCount}
";
$ Start ++;
/** Content on the right of the current page **/
Foreach ($ list1 as $ item ){
$ Url = $ this-> pageName. "-". $ item ['id']. ". html ";
$ Pages. ="
- {$ Start}
";
$ Start ++;
}
/** Next Page link **/
$ C = count ($ list2 );
If ($ c> 0 ){
$ Url = $ this-> pageName. "-". $ list2 [0] ['id']. ". html ";
$ Pages. = "Next page
";
} Else {
$ Pages. = "";
}
Return $ pages;
}
};
?>