如何配置分页codeigniter?

Posted

技术标签:

【中文标题】如何配置分页codeigniter?【英文标题】:how to configure pagination codeigniter? 【发布时间】:2012-03-04 13:54:03 【问题描述】:

我尝试用CodeIgniter进行分页,按照Codeigniter的手册应该很简单,即使在示例中是这样的

« 第一个 最后一个 »

$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');

在视图中我只这样称呼它 pagination->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 文档说应该工作..)任何想法?

谢谢!

【问题讨论】:

AFAIK,这只是分页的一个例子。不是可以使用 CodeIgniter Pagination 类创建的实际分页。您可能需要扩展分页类并自己调整 create_links() 方法。 谢谢,知道如何自己创建它们吗? (我讨厌 codeigniter 给出了这些例子,他们甚至没有给出如何制作它的线索......) 您使用的是哪个 CodeIgniter 版本? codeigniter 2.3 什么时候出来的?还是实际上是2.2?我所看到的是,最新的是 2.1? 【参考方案1】:

生成示例显示的内容实际上非常简单。你只需要扩展分页的库来适应这个。我能够做到这一点。无论您显示多少页,它仍会显示第一个、最后一个、后退箭头和前进箭头。

如果您想始终显示 5 个页面,其中包含转发和返回的内容,您需要有那么多结果来填充该页面。然后将num_links 设置为在第 3 页之前和之后所需的值。所以它会是 2。如果你在第一页,我的更改使它在适用时显示 4 页。见下图。白色是当前页面。绿色是可用的页面。

希望我已经正确解释了所有内容,这对您有用。告诉我。

控制器

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

【讨论】:

我写过关于 Cdeigniter 分页的教程。请查看并提出您的建议cloudways.com/blog/pagination-in-codeigniter【参考方案2】:

您得到 1 2 3 &gt; 而不是 « First &lt; 1 2 3 4 5 &gt; Last » 的唯一原因是您的结果中没有足够的行来生成超过 3 个页面。

“First”和“Last”链接在您不需要时默认不会出现。从documentation的例子看不太清楚。

在您超过第 1 页之前,您不会获得“上一个”链接,该示例实际上在第 3 页上(“3”为粗体)。

由于您在配置中提供自己的模板而不是使用默认模板,因此您的实际结果会略有不同。

如果您想快速测试以查看更多链接,只需将您的 per_page 减少到较小的数字或在您的 total_rows 中包含更多行。显示的链接总数也可以用num_links配置。

【讨论】:

谢谢!!我将链接减少到 5 个(现在我只有 18 行数据)但是如果我将 total_rows 设置为一个高参数,就像你说的那样,我会得到第一个和最后一个的链接,如果我点击最后一个我去该页面(第 1000 页)并且没有数据,还有其他方法可以模拟吗? 您只需要更多数据。将total_rows 设置为任意数字仅用于快速测试以查看链接,这不是您真正想要做的事情。 谢谢,我已经在 1000 上设置了 total_rows,但是当我点击“最后一个”链接时,我会转到第 1000 页...没有数据...知道如何解决它? 这可能是您查询数据库以生成实际结果的方式的问题(不是分页计数,这是一个单独的查询)。无论如何,它是separate question。我们必须查看您的控制器代码以及您如何查询数据库。 我将控制器设置为我的问题的更新,我尝试了两种方式,total_rows= 1000 到数据库,其他只到分页,但我仍然遇到同样的问题【参考方案3】:

对于那些使用 PostgreSql 开发他们的 CI 应用程序并且无法理解为什么分页限制+偏移的工作“奇怪”的人:

C控制器:

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

M模型:

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

【讨论】:

以上是关于如何配置分页codeigniter?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Codeigniter 的分页类中隐藏页面链接?

codeigniter分页类中的自定义查询

CodeIgniter 中分页的自动加载配置不起作用

如何在 Codeigniter 中创建分页?

如何使用 codeigniter 分页库回显页码?

如何使用 pjax 实现 Codeigniter 默认分页