2012-02-13 48 views
6

伊夫試圖與笨分頁,應該根據笨的手動那麼簡單,即使是在例如是這樣如何配置分頁codeigniter?

«第一< 1 2 3 4 5>最後»

$config['total_rows'] = $this->searchdesc_model->queryallrows(); 
$config['per_page'] = '10'; 
$config['uri_segment'] =4; 
$config['full_tag_open'] = '<p>'; 
$config['full_tag_close'] = '</p>'; 
$config['cur_tag_open'] = '<b>'; 
$config['cur_tag_close'] = '</b>'; 
$config['first_link'] = 'First'; 
$config['last_link'] = 'Last'; 
$config['last_tag_open'] = '<p>'; 
$config['last_tag_close'] = '</p>' 

$this->load->library('Company_Creation'); 

在視圖中我只把它叫做這樣的分頁 - > create_links(); ?>(或者我通過時,我把它從控制器的觀點發送,還是我只得到這個

1 2 3>

,有沒有辦法讓它看起來像〔實施例,聽起來所以假,但任何人都可以幫我嗎?還是有相似?

感謝

編輯1

$config['total_rows'] = $this->searchdesc_model->queryallrows(); 
$config['per_page'] = '5'; 
$config['uri_segment'] =4; 
$config['full_tag_open'] = '<p>'; 
$config['full_tag_close'] = '</p>'; 
$config['cur_tag_open'] = '<b>'; 
$config['cur_tag_close'] = '</b>'; 
$config['first_link'] = ' First'; 
$config['last_link'] = ' Last'; 
$config['last_tag_open'] = '<p>'; 
$config['last_tag_close'] = '</p>'; 
$config['next_link'] = ''; 
$config['next_tag_open'] = '<p id="nextbutton" style="padding-left:5px;">'; 
$config['next_tag_close'] = '</p>'; 
$config['prev_link'] = ''; 
$config['prev_tag_open'] = '<p id="prevbutton" style="padding-right:5px;">'; 
$config['prev_tag_close'] = '</p>'; 
$config['num_links']=4; 
$data['retorno'] = $this->searchdesc_model->queryalldb($config['per_page'],$this->uri->segment(4,0)); 
$config['total_rows']=1000; 
$this->pagination->initialize($config); 

一個問題,我這樣做是根據一些建議伊夫收到,就像你說的那樣,當許多數據工作的很好時,我仍然喜歡隨時顯示第一個和下一個按鈕,在我的查詢(我用正確的行數調用)之後設置了total_rows,並且之前我也嘗試過結果是一樣的,我也只需要顯示4個數字,我很討厭numb_links ...仍然不起作用(我不知道爲什麼Ci文件應該工作..)任何想法?

謝謝!

+0

據我所知,這只是看上去什麼像分頁的例子。不是使用CodeIgniter分頁類可以創建的實際分頁。您可能需要擴展分頁類並自己調整'create_links()'方法。 – 2012-02-13 05:12:40

+0

謝謝,有關如何由我自己創建它們的任何想法? (我討厭codeigniter給出了這些例子,他們甚至不提供如何使它的線索......) – jpganz18 2012-02-13 05:18:02

+0

你正在使用哪個CodeIgniter版本? – 2012-02-13 05:29:19

回答

9

生成示例所示的內容其實很簡單。你只需要擴展分頁的庫來適應這一點。我能夠做到這一點。無論顯示多少頁面,它仍然顯示首先,最後,後退箭頭和前進箭頭。

如果你想要隨時顯示5頁的正面和背面的東西,你需要有這麼多的結果來填充該頁面。然後在第3頁上設置num_links之前和之後的值。所以它會是2.如果您在第一頁上,我的更改會在適用後顯示4頁。見下圖。白色是當前頁面。綠色是可用的頁面。

enter image description here enter image description here

希望我一切正確的解釋,這對你的作品。讓我知道。

控制器

$this->pagingConfig = array(); 
    $this->pagingConfig['base_url'] = 'URL'; 
    $this->pagingConfig['total_rows'] = 0;//TOTAL ROWS 
    $this->pagingConfig['cur_page'] = 0;//CURRENT PAGE NUMBER 
    $this->pagingConfig['per_page'] = 0;//YOUR RESULTS PER PAGE 
    $this->pagingConfig['num_links'] = 2;//NUMBER OF LINKS BEFORE AND AFTER CURRENT PAGE IF ON PAGE ONE WILL SHOW 4 PAGES AFTERWARDS IF YOU HAVE ENOUGH RESULTS TO FILL THAT MANY 
    $this->pagingConfig['first_link'] = "&lt;&lt; First"; 
    $this->pagingConfig['last_link'] = "Last &gt;&gt;"; 
    $this->pagingConfig['full_tag_open'] = "<div class='pagination'>"; 
    $this->pagingConfig['full_tag_close'] = "</div>"; 
    $this->pagingConfig['last_tag_open'] = ""; 
    $this->pagingConfig['first_tag_close'] = ""; 
    $this->pagingConfig['anchor_class'] = "page"; 
    $this->pagination->initialize($this->pagingConfig); 
    $strPaging = $this->pagination->create_links(); 

擴展分頁庫調用

function create_links() 
{ 
    // EDIT: ADDED THIS BECAUSE COULDN'T SEEM TO SET THIS ANYWHERE ELSE 
    if ($this->anchor_class != '') 
    { 
    $this->anchor_class = 'class="'.$this->anchor_class.'" '; 
    } 

    // If our item count or per-page total is zero there is no need to continue. 
    if ($this->total_rows == 0 OR $this->per_page == 0) 
    { 
    return ''; 
    } 

    // Calculate the total number of pages 
    $num_pages = ceil($this->total_rows/$this->per_page); 

    // Is there only one page? Hm... nothing more to do here then. 
    if ($num_pages == 1) 
    { 
    return ''; 
    } 

    // Determine the current page number. 
    $CI =& get_instance(); 

    if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE) 
    { 
    if ($CI->input->get($this->query_string_segment) != 0) 
    { 
     $this->cur_page = $CI->input->get($this->query_string_segment); 

     // Prep the current page - no funny business! 
     $this->cur_page = (int) $this->cur_page; 
    } 
    } 
    else 
    { 
    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; 
    } 
    } 

    $this->num_links = (int)$this->num_links; 

    if ($this->num_links < 1) 
    { 
    show_error('Your number of links must be a positive number.'); 
    } 

    if (! is_numeric($this->cur_page)) 
    { 
    $this->cur_page = 1; 
    } 

    // Is the page number beyond the result range? 
    // If so we show the last page 
    if ($this->cur_page > $this->total_rows) 
    { 
    $this->cur_page = ($num_pages - 1); 
    } 

    // EDIT: DON'T NEED THIS THE WAY I'VE CHANGED IT 
    // $uri_page_number = $this->cur_page; 
    // $this->cur_page = floor(($this->cur_page/$this->per_page) + 1); 

    // EDIT: START OF MODIFIED START AND END TO WORK HOW I WANT 
    $totalLinks = ($this->num_links*2)+1; 
    if($totalLinks > ($this->total_rows/$this->per_page)) 
    { 
    $totalLinks = ceil($this->total_rows/$this->per_page); 
    } 
    //first page 
    if($this->cur_page == 1) 
    { 
    $start = 1; 
    $end = $start + $totalLinks - 1; 
    } 
    //middle pages 
    elseif($this->cur_page + $this->num_links <= $num_pages && $this->cur_page - $this->num_links > 0) 
    { 
    $start = $this->cur_page - $this->num_links; 
    $end = $this->cur_page + $this->num_links; 
    } 
    //last couple of pages 
    elseif(($this->cur_page + $totalLinks) > $num_pages) 
    { 
    $start = $num_pages - $totalLinks + 1; 
    $end = $num_pages; 
    //check to see if this is in the first half of links so it doesn't jump the paging 
    if($this->cur_page <= $this->num_links) 
    { 
     $start = 1; 
     $end = $start + $totalLinks - 1; 
    } 
    } 
    //first couple of pages 
    elseif(($this->cur_page - $totalLinks) < 1) 
    { 
    $start = 1; 
    $end = $start + $totalLinks - 1; 
    } 
    // EDIT: END OF MODIFIED START AND END TO WORK HOW I WANT 

    // EDIT: CODEIGNITERS BASE PAGING SETUP SEE ABOVE FOR MY CHANGES 
    // $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1; 
    // $end = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages; 

    // Is pagination being used over GET or POST? If get, add a per_page query 
    // string. If post, add a trailing slash to the base URL if needed 
    if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE) 
    { 
    $this->base_url = rtrim($this->base_url).'&amp;'.$this->query_string_segment.'='; 
    } 
    else 
    { 
    $this->base_url = rtrim($this->base_url, '/') .'/'; 
    } 

    // And here we go... 
    $output = ''; 

    // Render the "First" link 
    // EDIT: CHANGED TO ALWAYS SHOW FIRST LINK AT LEAST 
    if ($this->first_link !== FALSE AND $this->cur_page != 1) 
    { 
    $first_url = ($this->first_url == '') ? $this->base_url."1" : $this->first_url; 
    $output .= $this->first_tag_open.'<a '.$this->anchor_class.'href="'.$first_url.'">'.$this->first_link.'</a>'.$this->first_tag_close; 
    } 
    else 
    { 
    $output .= $this->cur_tag_open.$this->first_link.$this->cur_tag_close; 
    } 

    // Render the "previous" link 
    // EDIT: CHANGED TO ALWAYS SHOW PREVIOUS LINK AT LEAST 
    if ($this->prev_link !== FALSE AND $this->cur_page != 1) 
    { 
    $i = $this->cur_page-1; 

    if ($i == 0 && $this->first_url != '') 
    { 
     $output .= $this->prev_tag_open.'<a '.$this->anchor_class.'href="'.$this->first_url.'">'.$this->prev_link.'</a>'.$this->prev_tag_close; 
    } 
    else 
    { 
     $i = ($i == 0) ? '' : $this->prefix.$i.$this->suffix; 
     $output .= $this->prev_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$i.'">'.$this->prev_link.'</a>'.$this->prev_tag_close; 
    } 

    } 
    else 
    { 
    $output .= $this->cur_tag_open.$this->prev_link.$this->cur_tag_close; 
    } 

    // EDIT: CHANGED THIS TO ALWAYS SHOW ALL LINKS WANTED EVEN IF ON FIRST PAGE 
    // Render the pages 
    if ($this->display_pages !== FALSE) 
    { 
    // Write the digit links 
    for ($loop = $start; $loop <= $end; $loop++) 
    { 
     // EDIT: DON'T NEED THIS THE WAY I'VE CHANGED IT 
     // $i = ($loop * $this->per_page) - $this->per_page; 

     if ($loop >= 0) 
     { 
      if ($this->cur_page == $loop) 
      { 
       $output .= $this->cur_tag_open.$loop.$this->cur_tag_close; // Current page 
      } 
      else 
      { 
       $n = ($loop == 0) ? '0' : $loop; 

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

    // Render the "next" link 
    // EDIT: CHANGED TO ALWAYS SHOW NEXT LINK AT LEAST 
    if ($this->next_link !== FALSE AND $this->cur_page < $num_pages) 
    { 
    $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; 
    } 
    else 
    { 
    $output .= $this->cur_tag_open.$this->next_link.$this->cur_tag_close; 
    } 

    // Render the "Last" link 
    // EDIT: CHANGED TO ALWAYS SHOW LAST LINK AT LEAST 
    if ($this->last_link !== FALSE AND $this->cur_page != $num_pages) 
    { 
    $i = (($num_pages)); 
    $output .= $this->last_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$this->prefix.$i.$this->suffix.'">'.$this->last_link.'</a>'.$this->last_tag_close; 
    } 
    else 
    { 
    $output .= $this->cur_tag_open.$this->last_link.$this->cur_tag_close; 
    } 

    // Kill double slashes. Note: Sometimes we can end up with a double slash 
    // in the penultimate link so we'll kill all double slashes. 
    $output = preg_replace("#([^:])//+#", "\\1/", $output); 

    // Add the wrapper HTML if exists 
    $output = $this->full_tag_open.$output.$this->full_tag_close; 

    return $output; 
} 
+0

我寫了關於Cdeigniter分頁的教程。請看看並提出建議https://www.cloudways.com/blog/pagination-in-codeigniter/ – 2017-11-24 14:02:07

5

您得到1 2 3 >而不是« First < 1 2 3 4 5 > Last »的唯一原因是您的結果中沒有足夠的行來生成超過3頁。

  • 如果您不需要它們,默認情況下不會顯示「第一個」和「最後一個」鏈接。從documentation的例子中我們不太清楚。

  • 直到您超過第1頁,該示例實際上位於第3頁(「3」是粗體)之前,您將不會獲得「Previous」鏈接。

  • 由於您在配置中提供了自己的模板而不是使用默認值,因此實際結果會稍有不同。

如果你想要做一個快速的測試,看看更多的聯繫,只是減少你per_page到一個較低的數字或包括在total_rows更多的行。顯示的鏈接總數也可配置爲num_links

+0

謝謝!我減少了鏈接到5(現在我只有18行數據),但如果我設置total_rows一個高參數我得到,就像你說的,第一和最後的鏈接,問題,如果我點擊最後我去該頁面(頁碼1000),它沒有數據,任何其他方式來模擬? – jpganz18 2012-02-13 05:39:15

+1

你只需要更多的數據。將'total_rows'設置爲任意數字僅用於快速測試以查看鏈接,這不是您實際想要執行的操作。 – 2012-02-13 05:51:34

+0

謝謝,我已經在1000上設置了total_rows,但是當我點擊「最後」鏈接發送給頁面1000 ...沒有數據......任何想法如何解決它? – jpganz18 2012-02-13 06:12:09

0

對於那些誰發展與PostgreSQL的他們的CI應用程序,不明白爲什麼分頁限制+偏移作品「奇怪的人「:

C ontroller:

... 
$offset = ($page-1)*$config["per_page"]; 
$this->reporting_model->some_fetch_method($id, $config["per_page"], $offset); 
... 

中號奧德爾:

... 
$this->db->limit($limit_perpage, $offset); 
$this->db->where("id", $id); 
$this->db->get('some_table'); 
...