2012-01-28 81 views
5

我的控制器功能分頁沒有正確顯示頁碼笨

function test($start_from = 0) 
{ 
    $this->load->library('pagination'); 

    $data = array(); 

    $per_page = 3; 
    $total = $this->activity_model->count_by(); 

    $config['base_url'] = base_url() . 'test'; 
    $config['total_rows'] = $total; 
    $config['per_page'] = $per_page; 
    $config['uri_segment'] = 2; 
    $config['num_links'] = 2; 
    $config['use_page_numbers'] = TRUE; 

    $data['follow'] = $this->activity_model->get($per_page, $start_from); 

    $this->pagination->initialize($config); 

    $data['pagination'] = $this->pagination->create_links(); 

    $this->load->view('front_end/test' ,$data); 
} 

我的路線:

$route['test'] = "user_activity/test"; 
$route['test/(:any)'] = "user_activity/test/$1"; 

型號:

function get($limit,$start_from) 
{ 
$sql = "SELECT * FROM user_follow LIMIT $start_from, $limit"; 

    $query = $this->db->query($sql); 
    return $query->result_array(); 
} 

問題是,我有分頁1,2, 3,4,5 ....並在每頁顯示3項。我想要做的是,在URL它顯示我的頁號1,2,3,4,5

當我點擊第二頁的URL顯示3 當我點擊第三頁的URL顯示6等+3

是否有可能,我花了幾個小時在互聯網上尋找建議,但我沒有理解[code] $ config ['use_page_numbers'] = TRUE; [/ code]做我需要的,但在我的情況下它仍然不起作用。

也許你可以建議任何圖書館?

回答

7

在分頁類(/system/libraries/Pagination.php)中進行以下更改,以便它使用頁碼而不是偏移量。

OLD(行146-153):

if ($CI->uri->segment($this->uri_segment) != 0) 
{ 
    $this->cur_page = $CI->uri->segment($this->uri_segment); 

    // Prep the current page - no funny business! 
    $this->cur_page = (int) $this->cur_page; 
} 

新:

添加「其他」選項的if語句,以確保默認爲;頁= 1

if ($CI->uri->segment($this->uri_segment) != 0) 
{ 
    $this->cur_page = $CI->uri->segment($this->uri_segment); 

    // Prep the current page - no funny business! 
    $this->cur_page = (int) $this->cur_page; 
} 
else 
{ 
    $this->cur_page = 1; 
} 

OLD(線175):

$this->cur_page = floor(($this->cur_page/$this->per_page) + 1); 

NEW:

只需註釋出該行以便當前頁面服從控制器/ URI。

//$this->cur_page = floor(($this->cur_page/$this->per_page) + 1); 

OLD(線206):

$i = $uri_page_number - $this->per_page; 

NEW:

前頁應該總是當前頁面由1減去

$i = $uri_page_number - 1; 

OLD(線230):

if ($this->cur_page == $loop) 

NEW:

的URI缺少分頁應該被認爲第1頁。

if ($this->cur_page == $loop || ($this->cur_page == 1 && $this->cur_page == $loop)) 

OLD(線238-247):

if ($n == '' && $this->first_url != '') 
{ 
    $output .= $this->num_tag_open.'<a '.$this->anchor_class.'href="'.$this->first_url.'">'.$loop.'</a>'.$this->num_tag_close; 
} 
else 
{ 
    $n = ($n == '') ? '' : $this->prefix.$n.$this->suffix; 

    $output .= $this->num_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$n.'">'.$loop.'</a>'.$this->num_tag_close; 
} 

新:

頁面URL應該使用頁碼和不偏移。

if ($n == '' && $this->first_url != '') 
{ 
    $output .= $this->num_tag_open.'<a '.$this->anchor_class.'href="'.$loop.'">'.$loop.'</a>'.$this->num_tag_close; 
} 
else 
{ 
    $n = ($n == '') ? '' : $this->prefix.$n.$this->suffix; 

    $output .= $this->num_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$loop.'">'.$loop.'</a>'.$this->num_tag_close; 
} 

OLD(線256):

$output .= $this->next_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$this->prefix.($this->cur_page * $this->per_page).$this->suffix.'">'.$this->next_link.'</a>'.$this->next_tag_close; 

NEW:

下一頁應該總是當前頁面和總和1.

$output .= $this->next_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$this->prefix.($this->cur_page + 1).$this->suffix.'">'.$this->next_link.'</a>'.$this->next_tag_close; 

OLD(線262):

$i = (($num_pages * $this->per_page) - $this->per_page); 

新:

末頁應該是總頁數。

$i = $num_pages; 

用新行替換所有舊行。確保在更改之前進行文件備份。

希望這有助於:)

編輯:

你需要更新你的控制器功能測試,如:

function test($start_from = 0) 
{ 
    $this->load->library('pagination'); 

    $data = array(); 

    $per_page = 3; 
    $total = $this->activity_model->count_by(); 

    $config['base_url'] = base_url() . 'test'; 
    $config['total_rows'] = $total; 
    $config['per_page'] = $per_page; 
    $config['uri_segment'] = 2; 
    $config['num_links'] = 2; 
    $config['use_page_numbers'] = TRUE; 

    $start = $per_page * ($start_from-1); 

    $data['follow'] = $this->activity_model->get($per_page, $start); 

    $this->pagination->initialize($config); 

    $data['pagination'] = $this->pagination->create_links(); 

    $this->load->view('front_end/test' ,$data); 
} 

在這裏,我已經添加了一個新的變量$開始是$per_page * ($start_from-1) 。現在將這個$ start作爲參數傳遞給model。

該做的是什麼乘以每頁的項目數與(當前頁號-1)。這意味着,如果每頁您的項目是10,你是第二頁的$start = 10 *(2-1)這給10上。所以,你的結果會從10,20開始,所以一個

希望這有助於:)

+0

將這個去笨2.0.3?無論如何謝謝你! – Viktors 2012-01-28 20:41:59

+0

行號會有一些小的變化。任何方式試一試。我的是一箇舊版本。請讓我知道它是否有效:) – Sabari 2012-01-29 03:07:50

+0

這件事情對你有用嗎? – Sabari 2012-01-29 10:06:46

11

我設法做到這一點,而無需修改類。最好的方法是製作分頁課程的副本,進行更改並使用它。這種方式如果你更新配置項,你將不會失去修改。這是我的解決方案,無需修改類。

首先我想說的是,僅使用配置選項$config['use_page_numbers'] = TRUE也可以做到這一點,但不是完全的。我發現不能使用這個選項的東西如下:
如果您嘗試手動編輯網址欄頁面,它會將它們視爲偏移而不是頁面,並且如果您嘗試使用頁面2返回到頁面1 「prev」鏈接也會將頁碼視爲偏移量。

代碼:

$config['base_url'] = base_url('my/url/page'); 
$config['total_rows'] = count($this->my_model->get_all()); 
$config['per_page'] = 2; 
$config['use_page_numbers'] = TRUE;  
$config['uri_segment'] = 4; 

//i'm looading the pagination in the constuctor so just init here 
$this->pagination->initialize($config); 

if($this->uri->segment(4) > 0) 
    $offset = ($this->uri->segment(4) + 0)*$config['per_page'] - $config['per_page']; 
else 
    $offset = $this->uri->segment(4); 
//you should modify the method in the model to accept limit and offset or make another function - your choice  
$data['my_data'] = $this->my_model->get_all($config['per_page'], $offset); 

這樣
頁= FALSE(我/ URL)或(我/網址/網頁) - 基本上,如果第四URI段是假的,
頁= 0(我/ URL /頁/ 0),

頁= 1(我/ URL /頁面/ 1)

將全部顯示的第一頁,然後將其它鏈接將被正常工作。我也在驗證頁面,例如 - 如果有人想輸入(my/url/page/2323),這會拋出一個錯誤,在模型中你應該檢查結果是否爲假,如果是控制器應該顯示錯誤頁面或其他東西。希望這可以幫助。

+0

這一個解決了我的問題tnx – Ramje 2013-10-17 07:11:57

0

你能避免PHP錯誤,如果有人手動輸入URL中的頁面數:

例如我/網址/網頁/ 786 你的結果可能不會有什麼顯示查詢786,默認情況下它會告訴你PHP的error..to避免這種情況,你可以使用:

if (isset($result)) { 
    $isArray = is_array($result) ? "YES" : "NO"; //check that result is an array or not as per your requirement. 
    if ($isArray == "YES") { 
    echo "show your stuffs here.."; 
    } 
    else{ 
    echo "display your message instead for php error here.."; 
    } 
} 

希望這有助於... 查詢鳴叫我在twitter上@sufiyantech