0

我有这个分页类,自从我开始学习 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,我希望在屏幕上看到一些分页。

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

4

3 回答 3

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;
    }
}
于 2013-09-19T08:49:29.393 回答
0

你做

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

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

于 2013-09-18T09:41:04.323 回答
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);

结果: 使用分页查看结果

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

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);
于 2016-11-23T09:59:54.160 回答