PHP分页显示结果

时间:2015-03-21 09:53:45

标签: php

我使用下面的PHP代码在PHP搜索页面上显示以下文本,每页有6个结果。

显示x结果的x-x。

这样可行,但第一页显示:显示0-6而不是1-6的x结果,我在代码中更改此内容时遇到了问题。

如果没有找到结果,我还需要更改消息。即。显示0结果

 echo $paginate;  
  }
public function addPaginationInfo() {
$lastpage = ceil($this->total_pages/$this->limit);   
$LastPagem1 = $lastpage - 1;
$paginate = '<div><div style="color:#333">Showing<span style="color:#3E7CB3"> '.$this->limit_sql_start.'-'.$this->limit_sql_end.' </span>of <span style="color:#3E7CB3">'.$this->total_pages.'</span> Results</div></div>';
echo $paginate; 
}

这是完整的分页代码

public function executePagination(){

$db = DB::get_db();
$this->buildQuery();

$db->query(self::BASEQUERY,
  $this->fields_sql,
  $this->table_sql,
  $this->where_sql,
  $this->order_sql,
  $this->limit_sql);

$this->total_pages = $db->num_rows();
if(isset($_GET['page'])) {
$page = strip_tags(mysql_escape_string($_GET['page']));
} else { $page = FALSE; }    
if($page){
$start = ($page - 1) * $this->limit; 
} else {
$start = 0;  
}
$this->limit_sql_start = $start;
if($this->total_pages > $this->limit+$start) {
$this->limit_sql_end = $this->limit+$start; 
} else {
$this->limit_sql_end = $this->total_pages; 
}
$this->setLimit(" LIMIT $start, $this->limit "); 

$db->query(self::BASEQUERY,
  $this->fields_sql,
  $this->table_sql,
  $this->where_sql,
  $this->order_sql,
  $this->limit_sql);

  return $db->fetch_rows();
  }
  public function addPaginationLinks() {
  $targetpage = "";
  if(isset($_GET['page'])) {
  $page = strip_tags(mysql_escape_string($_GET['page']));
  } else { $page = FALSE; }
if ($page == 0){$page = 1;}
$prev = $page - 1;  
$next = $page + 1;              
$lastpage = ceil($this->total_pages/$this->limit);    
$LastPagem1 = $lastpage - 1;

$paginate = '';
if($lastpage > 1)
{  
$paginate .= "<div class='paginate'>";

if ($page > 1){
$paginate.= "<a href='$targetpage?page=$prev'>Back</a>";
}else{
$paginate.= "";  }
if ($lastpage < 7 + ($this->stages * 2)) 
{  
for ($counter = 1; $counter <= $lastpage; $counter++)
{
  if ($counter == $page){
    $paginate.= "<a class='current'>$counter</a>";
  }else{
    $paginate.= "<a  href='$targetpage?page=$counter'>$counter</a>";}          
}
}
 elseif($lastpage > 5 + ($stages * 2)) 
{
if($page < 1 + ($this->stages * 2))    
{
  for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
  {
    if ($counter == $page){
      $paginate.= "<a class='current'>$counter</a>";
    }else{
      $paginate.= "<a  href='$targetpage?page=$counter'>$counter</a>";}          
  }
  $paginate.= "...";
  $paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
  $paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";    
  }
  elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
  {
  $paginate.= "<a href='$targetpage?page=1'>1</a>";
  $paginate.= "<a href='$targetpage?page=2'>2</a>";
  $paginate.= "...";
  for ($counter = $page - $stages; $counter <= $page + $stages;  $counter++)
  {
    if ($counter == $page){
      $paginate.= "<a class='current'>$counter</a>";
    }else{
      $paginate.= "<a  href='$targetpage?page=$counter'>$counter</a>";}          
  }
  $paginate.= "...";
  $paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
  $paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";    
}
else
{
  $paginate.= "<a href='$targetpage?page=1'>1</a>";
  $paginate.= "<a href='$targetpage?page=2'>2</a>";
  $paginate.= "...";
  for ($counter = $lastpage - (2 + ($stages * 2)); $counter <=  $lastpage; $counter++)
  {
    if ($counter == $page){
      $paginate.= "<a class='current'>$counter</a>";
    }else{
      $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}          
  } } }
  if ($page < $counter - 1){ 
 $paginate.= "<a href='$targetpage?page=$next'>Next</a>";
 }else{
$paginate.= "<a class='disabled'>Next</a>";
}
$paginate.= "</div>";    
 }

1 个答案:

答案 0 :(得分:0)

更改此行

$paginate = '<div><div style="color:#333">Showing<span style="color:#3E7CB3"> '.$this->limit_sql_start.'-'.$this->limit_sql_end.' </span>of <span style="color:#3E7CB3">'.$this->total_pages.'</span> Results</div></div>';

要,

if($this->limit_sql_end > 0)
{
    $paginate = '<div><div style="color:#333">Showing<span style="color:#3E7CB3"> '.$this->limit_sql_start.'-'.($this->limit_sql_end + 1).' </span>of <span style="color:#3E7CB3">'.$this->total_pages.'</span> Results</div></div>';
}
else
    $paginate = '<div><div style="color:#333">Showing 0 Results</div></div>';