Php achieves similar product pages through Alibaba

Source: Internet
Author: User
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. ="
    1. {$ Start}
    2. ";
      $ Start ++;
      }

      $ Pages. ="
    3. {$ LeftCount}
    4. ";
      $ Start ++;

      /** Content on the right of the current page **/
      Foreach ($ list1 as $ item ){
      $ Url = $ this-> pageName. "-". $ item ['id']. ". html ";
      $ Pages. ="
    5. {$ Start}
    6. ";
      $ Start ++;
      }

      /** Next Page link **/
      $ C = count ($ list2 );
      If ($ c> 0 ){
      $ Url = $ this-> pageName. "-". $ list2 [0] ['id']. ". html ";
      $ Pages. = "Next page
        ";
        } Else {
        $ Pages. = "";
        }

        Return $ pages;
        }
        };
        ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.