Home  >  Article  >  Backend Development  >  How to display the Baidu paging effect (under the search page)?

How to display the Baidu paging effect (under the search page)?

黄舟
黄舟Original
2017-07-20 09:40:503257browse

1: Data collection

$pageIndex = $_GET['p']?$_GET['p']:'1';//当前页码    
$perpage = 10;//每页显示多少数量,和页码无关    
$count = 121;//总数是多少,//和页码无关    
$flag =9;//显示页码个数    
$pageCount = ceil($count/$perpage); //总的页码个数
   $pagesInRange = $this->page_show($pageIndex, $pageCount, $flag);//显示的页码数组

Note: page_show is a function used to construct an array of displayed page number data, as shown below.

public function page_show2($pageIndex, $pageCount, $flag){
  if($flag > $pageCount){             
  $flag = ( $pageCount);           }
  if($flag%2){            
  $aa = floor($flag/2);            
  $lowerBound = $pageIndex - $aa;        
  $upperBound = $pageIndex + $aa;          
  }else{
           $aa = floor($flag/2);            
           $lowerBound = $pageIndex - $aa;        
           $upperBound = $pageIndex + $aa -1;          
           }
     if( $lowerBound< 1){        
     $offset = 0-$lowerBound +1;        
     $lowerBound =1;        
     $upperBound = $upperBound + $offset;      
     }
     if($upperBound> $pageCount){
       $up = $upperBound;        
       $upperBound = $pageCount;
       $offset = $up-$pageCount;        
       $lowerBound = $lowerBound - $offset;      
       }
     $data = array();      
     for ($i=($lowerBound); $i <= ($upperBound); $i++) {         
     $data[] = $i;      
     }
     return $data;     
     }

After the data is constructed, you need to use some data to make judgments to display which page numbers.

The data is used in the html page as follows: (Note: The variables with $this-> are the variables collected before. They are just written differently on the html page. Basically, they are written like this in the framework. If there is no framework, you have to echo the following data yourself, and remove the $this-> character in the variable)

<?php if ($pageCount > 1): ?>
<?php if (($pageIndex > 1)): ?>
<span class="page_up"><a href="<?php echo $link . &#39;&p=&#39; . ($pageIndex-1) ?>">上一页</a></span>
<?php endif; ?>
<?php foreach($pagesInRange as $page) : ?>
<?php if ($page == $pageIndex): ?>
<span class="curr onthis"><?php echo $page;?></span>
<?php else : ?>
<span><a href="<?php echo $link . &#39;&p=&#39; . $page ?>"><?php echo $page;?></a></span>
<?php endif;?>
<?php endforeach;?>
<?php if (($pageIndex< $pageCount)): ?>
<span class="page_next"><a href="<?php echo $link . &#39;&p=&#39; . ($pageIndex +1) ?>">下一页</a></span>
<?php endif; ?>
<?php endif;?>

The effect is as follows: the pagination below is similar to the Baidu search page. The style You have to modify it yourself.

The above is the detailed content of How to display the Baidu paging effect (under the search page)?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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