Home > Backend Development > PHP Tutorial > PHP implements imitation of Alibaba to realize page turning of similar products_PHP tutorial

PHP implements imitation of Alibaba to realize page turning of similar products_PHP tutorial

WBOY
Release: 2016-07-21 15:42:44
Original
1090 people have browsed it

If the number of records on the left is less than the value of $space (page number section), the page number $start increases 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 counting by subtracting the $left value from the number of records on the left.

Copy code The code is as follows:

/**Realize page turning 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 = "< a href="{$url}">Previous page
    ";
    }elseif($c == 1) {
    $url = $this->pageName ."-".$list1[0]['id'].".html";
    $pages = "Previous page< ;ol>";
    }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. < ;b>{$leftCount}
  4. ";
    $start++;

    /**Content on the right side of the current page **/
    foreach($list1 as $item) {
    $url = $this->pageName."-".$item['id'].".html";
    $pages .= "
  5. {$start}
  6. ";
    $start++;
    }

    /**Link to next page **/
    $ c = count($list2);
    if($c > 0) {
    $url = $this->pageName."-".$list2[0]['id'].". html";
    $pages .= "next page
      ";
      }else {
      $pages . = "";
      }

      return $pages;
      }
      };
      ?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320934.htmlTechArticleIf the number of records on the left is less than the value of $space (page number section), the page number $start starts from 1 and goes to Right value added. 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 be from the left...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template