PHP implementation of the alibaba implementation of similar products page turning

  • 2020-03-31 20:14:04
  • OfStack

If the number of record bars 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(left and right pages), $start counts from the number of records on the left minus the value of $left.

<?php 
 

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); 

 
$c = count($list1); 
if($c > 1) { 
$url = $this->pageName."-".$list1[$c]['id'].".html"; 
$pages = "<a href="{$url}"> The previous page </a><ol>"; 
}elseif($c == 1) { 
$url = $this->pageName."-".$list1[0]['id'].".html"; 
$pages = "<a href="{$url}"> The previous page </a><ol>"; 
}else { 
$pages = ""; 
} 


 
foreach($list1 as $item) { 
$url = $this->pageName."-".$item['id'].".html"; 
$pages .= "<li><a href="{$url}">{$start}</a></li>"; 
$start++; 
} 

$pages .= "<li><b>{$leftCount}</b></li>"; 
$start++; 

 
foreach($list1 as $item) { 
$url = $this->pageName."-".$item['id'].".html"; 
$pages .= "<li><a href="{$url}">{$start}</a></li>"; 
$start++; 
} 

 
$c = count($list2); 
if($c > 0) { 
$url = $this->pageName."-".$list2[0]['id'].".html"; 
$pages .= "<a href="{$url}"> The next page </a><ol>"; 
}else { 
$pages .= ""; 
} 

return $pages; 
} 
}; 
?> 

Related articles: