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

时间:2013-09-18 09:11:21

标签: php class oop pagination

我有这个分页类,我从正常的程序功能转换为类,因为我开始学习OOP。在旧的程序方式中,该功能工作正常,但我不能让这个新的类版本显示在屏幕上

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>'; 
    }

和另一个类中的其他地方我想创建该类的新实例并在返回时传递方法以及一些参数

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

当我尝试var_dump()时,它会显示为NULL,我希望在屏幕上看到一些分页。

为了能够显示分页,我需要改变什么?我是否必须在$ total_pages,$ page和$ webpage的Pagination类中创建一些变量,并在构造函数中初始化它们并从pagination_one方法中删除它们?

3 个答案:

答案 0 :(得分:0)

你做

return $this->paging->pagination_one...

当你没有在pagination_one -method中返回任何内容时,因此为null。

答案 1 :(得分:0)

我通过删除类中的私有变量并更改构造函数来自行修复它。

该课程现在看起来像这样

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 :(得分:0)

function pagination($sql_total_row, $post_per_page,$current_page=1, $url='', $lasturl = '', $parameter ='paging') {
    $number_page = ceil($sql_total_row / $post_per_page);if($number_page<=1) return false;
    $uls ='<ul class="pagination pagination-sm">';
    $a = parse_url($url);
    if(isset($a['query']))$url .= '&';else $url .= '?';
    $url .= $parameter.'=';

    $urls = '';
    $distanc = 5;
    $f = $current_page-$distanc;
    $l = $current_page+$distanc;
    $li = function($n,$link,$current_page){ return $current_page==$n ? '<li class="active"><span>'.$n.'</span><li>' : '<li><a href="'.$link.'">'.$n.'</a></li>'; };

    for ($i = $current_page; $i > 0; $i--){
        if($i>$f or $i < $distanc)
            $urls = $li($i,$url.$i.$lasturl,$current_page). $urls;
        else{
            $i = $distanc;
            $urls = '<li><span>...</span><li>'.$urls;
        }
    }

    for ($i = $current_page+1; $i < $number_page; $i++){
        if($i<$l or $i > $number_page - $distanc)
            $urls .= $li($i,$url.$i.$lasturl,$current_page);
        else{
            $i = $number_page - $distanc;
            $urls.= '<li><span>...</span><li>';
        }
    }
    return $uls.$urls.'</ul>';
}

用法:

$total_row_sql = 1500; //required - get from mysql: SELECT FOUND_ROWS();
$row_display = 50; //required
$parameter_paged = (isset($_GET['paging'])) ? $_GET['paging'] : 1; // required
$custom_url = '/wordpress_url/?'; //custom
$hash = '#target_element_id'; //custom
$name_paramerter_paging = 'paging'; //custom

echo pagination($total_row_sql,$row_display,$parameter_paged,$custom_url,$hash,$name_paramerter_paging);

结果: view result using pagination

=============================================== ========================= 如果使用数据库循环示例:

function select( $table, $field='*', $where='', $order='') {
    $sql = "select SQL_CALC_FOUND_ROWS $field from $table";
    if($where) $sql.= " where $where";
    if($order) $sql.= " order by $order";
    $items = $wordpress_mysql->get_results( $sql );// custom: default result object, if u want parse array: ( $sql, ARRAY_A);
    $sql_posts_total = $wordpress_mysql->get_var( "SELECT FOUND_ROWS();" );
    return (object)['result'=>$items, 'total_rows'=>$sql_posts_total];
}
$result = select('user');

$data_items = $result->result;// foreach show database if u want

$sql_posts_total = $result->total_rows;
$post_per_page = 50;
$parameter_paged = (isset($_GET['paging'])) ? $_GET['paging'] : 1;
echo pagination($sql_posts_total,$post_per_page,$parameter_paged);