doulou1989 2013-09-18 09:11
浏览 27
已采纳

PHP返回一个分页对象并在调用时显示链接

I have this pagination class which I converted from a normal procedural function to a class since I started learning OOP. In the old procedural way, the function worked fine but I can't get this new class version to display on the screen

class Pagination {
    public function __construct() {
    }

    /**
     * This function is called whenever the there are several records to be displayed in the table
     * This saves the page extending further down the page creating a long list of results
     * when all the results can be spread across multiple pages
     */
    public function pagination_one($total_pages, $page, $webpage) { 

        // Maximum number of links per page.  If exceeded, google style pagination is generated
        $max_links = 6; 
        $h=1; 
        if($page>$max_links) { 

            $h=(($h+$page)-$max_links); 
        } 
        if($page>=1) {
            $max_links = $max_links+($page-1); 
        } 
        if($max_links>$total_pages) {
            $max_links=$total_pages+1; 
        } 
        echo '<div class="page_numbers"> 
          <ul>'; 

        if($page>"1") { 
             echo '<li class="current"><a href="/'.$webpage.'/1">First</a></li> 
              <li class="current"><a href="/'.$webpage.'/'.($page-1).'">Prev</a></li> '; 
        } 

        if($total_pages!=1) {
                  for ($i=$h;$i<$max_links;$i++) { 
                if($i==$page) { 
                    echo '<li><a class="current">'.$i.'</a></li>'; 
                } 
                else 
                            { 
                    echo '<li><a href="/'.$webpage.'/'.$i.'">'.$i.'</a> </li>'; 
                } 
            } 
        } 

        if(($page >="1")&&($page!=$total_pages)) {

    echo '<li class="current"><a href="/'.$webpage.'/'.($page+1).'">Next</a></li> 
          <li class="current"><a href="/'.$webpage.'/'.$total_pages.'">Last</a></li>'; 
        } 
        echo '</ul> </div>'; 
    }

and elsewhere in another class I want to create a new instance of that class and pass the method in the return along with some parameters

public function paging() {
    if($this->getcount != 0) {
          $this->paging = new Pagination();
          return $this->paging->pagination_one($this->total_pages, $this->page, 'news');
        }
    }

When I try I var_dump() it comes up as NULL where I expect to see some pagination on the screen.

What have i got to change to be able to display the pagination? Do I have to created some variables in the Pagination class for $total_pages, $page and $webpage and initialise them in the constructor and remove them from the pagination_one method?

  • 写回答

3条回答 默认 最新

  • dougeqiang1619 2013-09-19 08:49
    关注

    I fixed it myself by removing the private variables in the class and changing the constructor.

    The class now looks like this

    class Pagination {
    
        public function __construct($total_pages, $page, $webpage) {
    
            $this->total_pages = $total_pages;
            $this->page = $page;
            $this->webpage = $webpage;
        }
    
        /**
         * This function is called whenever the there are several records to be displayed in the table
         * This saves the page extending further down the page creating a long list of results
         * when all the results can be spread across multiple pages
         */
        public function pagination_one() { 
    
            // Maximum number of links per page.  If exceeded, google style pagination is generated
            $max_links = 6; 
            $h = 1; 
            if($this->page > $max_links) { 
    
                $h=(($h + $this->page) - $max_links); 
            } 
            if($this->page >= 1) {
    
                $max_links = $max_links + ($this->page - 1); 
            } 
            if($max_links > $this->total_pages) {
    
                $max_links = $this->total_pages + 1; 
            } 
    
            $paging = '';
    
            $paging .= '<div class="page_numbers"> 
              <ul>'; 
    
            if($this->page > "1") { 
    
                $paging .= '<li class="current"><a href="/'.$this->webpage.'/1">First</a></li> 
                      <li class="current"><a href="/'.$this->webpage.'/'.($this->page - 1).'">Prev</a></li> '; 
            } 
    
            if($this->total_pages != 1) { 
    
                for ($i=$h; $i < $max_links; $i++) { 
    
                    if($i == $this->page) { 
    
                        $paging .= '<li><a class="current">'.$i.'</a></li>'; 
                    } 
                    else { 
    
                        $paging .= '<li><a href="/'.$this->webpage.'/'.$i.'">'.$i.'</a> </li>'; 
                    } 
                } 
            } 
    
            if(($this->page >= "1" ) && ($this->page != $this->total_pages)) {
    
                $paging .= '<li class="current"><a href="/'.$this->webpage.'/'.($this->page + 1).'">Next</a></li> 
                      <li class="current"><a href="/'.$this->webpage.'/'.$this->total_pages.'">Last</a></li>'; 
            } 
    
            $paging .= '</ul> </div>'; 
    
            return $paging;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法