如何配置分页codeigniter? [英] how to configure pagination codeigniter?

查看:134
本文介绍了如何配置分页codeigniter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



«第一< p> 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'] ='第一';
$ config ['last_link'] ='最后';
$ config ['last_tag_open'] ='< p>';
$ config ['last_tag_close'] ='< / p>'

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

在视图中我只称之为这个分页 - > create_links ?>(或者当我从控制器调用它时,我通过视图发送它,仍然只有这个



1 2 3>



并且没有办法让它看起来像exmaple,可能听起来这么虚拟,但任何人都可以帮助我这个吗?或有类似的问题吗?



感谢



编辑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 =nextbuttonstyle =padding-left:5px;>';
$ config ['next_tag_close'] ='< / p>';
$ config ['prev_link'] ='';
$ config ['prev_tag_open'] ='< p id =prevbuttonstyle =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);

我这样做是根据一些建议Ive收到,像你说的,当许多数据工作好,仍然我想爱显示第一个和下一个按钮,所有的时间,我设置total_rows后我的查询(我调用正确的行数),我也试过,结果是一样的,我还需要显示只有4数字和我ustust numb_links ...仍然不工作(我不知道为什么Ci docs说应该工作..)任何想法?



谢谢!

解决方案

生成示例显示的内容实际上很容易。你只需要扩展Pagination的库来适应这一点。我能做到这一点。无论您显示多少页面,它仍显示第一个,最后一个,返回箭头和向前箭头。



如果您想随时显示5页和后面的东西,你需要有很多结果来填充该页面。然后,您在第3页上将 num_links 设置为之前和之后的内容。所以它将是2.我的更改,如果你在第一页上,使其显示4页后适用。见下图。白色是当前页面。绿色是可用的页面。






希望我已经解释一切正确,这适用于你。让我知道。



管理员

  $ this-> pagingConfig = array(); 
$ this-> pagingConfig ['base_url'] ='URL';
$ this-> pagingConfig ['total_rows'] = 0; // TOTAL ROWS
$ this-> pagingConfig ['cur_page'] = 0; //当前页码
$ this-> pagingConfig ['per_page'] = 0; //每页的结果
$ this-> pagingConfig ['num_links'] = 2; //当前页面之前的链接数如果您有足够的结果填充许多$​​ b $ b $ this-> pagingConfig ['first_link'] =& lt;& lt;第一;
$ this-> pagingConfig ['last_link'] =上次& 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();

延伸分页库

 函数create_links()
{
//编辑:添加这是因为不能设置任何
if this-> anchor_class!='')
{
$ this-> anchor_class ='class ='。$ this-> anchor_class。'';
}

//如果我们的项目计数或每页总计为零,则不需要继续。
if($ this-> total_rows == 0 OR $ this-> per_page == 0)
{
return'';
}

//计算总页数
$ num_pages = ceil($ this-> total_rows / $ this-> per_page);

//只有一个页面?嗯...没有什么比这里做的更多。
if($ num_pages == 1)
{
return'';
}

//确定当前页码。
$ 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);

//准备当前页面 - 没有有趣的业务!
$ 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);

//准备当前页面 - 没有有趣的业务!
$ this-> cur_page =(int)$ this-> cur_page;
}
}

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

if($ this-> num_links< 1)
{
show_error('您的链接数必须是正数。
}

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

//页码是否超出结果范围?
//如果是这样我们显示最后一页
if($ this-> cur_page> $ this-> total_rows)
{
$ this-& ($ num_pages - 1);
}

//编辑:不需要这个方法我改变了
// $ uri_page_number = $ this-> cur_page;
// $ this-> cur_page = floor(($ this-> cur_page / $ this-> per_page)+ 1);

//编辑:开始修改的开始和结束工作我想要
$ totalLinks =($ this-> num_links * 2)+1;
if($ totalLinks>($ this-> total_rows / $ this-> per_page))
{
$ totalLinks = ceil($ this-> total_rows / $ this- > per_page);
}
//第一页
if($ this-> cur_page == 1)
{
$ start = 1;
$ end = $ start + $ totalLinks - 1;
}
//中间页
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;
}
//最后几页
elseif(($ this-> cur_page + $ totalLinks)> $ num_pages)
{
$ start = $ num_pages - $ totalLinks + 1;
$ end = $ num_pages;
//检查这是否在链接的前半部分,所以它不跳转页面
if($ this-> cur_page< = $ this-> num_links)
{
$ start = 1;
$ end = $ start + $ totalLinks - 1;
}
}
//第一对页面
elseif(($ this-> cur_page - $ totalLinks)< 1)
{
$ start = 1;
$ end = $ start + $ totalLinks - 1;
}
//编辑:修改的开始和结束工作我想要的

//编辑:CODEIGNITERS基本分页设置上面为我的更改
/ / $ 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;

//分页是通过GET还是POST使用?如果得到,添加一个per_page查询
//字符串。如果发布,如果需要,添加一个尾部斜杠到基本URL
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,'/')。
}

//这里我们去...
$ output ='';

//渲染第一个链接
//编辑:CHANGED TO ALWAYS SHOW FIRST LINK ATAST
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;
}

//渲染上一个链接
//编辑:CHANGED TO ALWAYS SHOW PROWIOUS链接最低
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;
}

//编辑:改变这总是显示所有链接,即使在第一页
//渲染页面
if($ this-> display_pages!== FALSE)
{
//写数字链接
for($ loop = $ start; $ loop< = $ end; $ loop ++)
{
//编辑:不需要这个方法我改变了
// $ 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; //当前页
}
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;
}
}
}
}
}

//渲染下一个链接
//编辑:CHANGED始终显示下一个链接最低
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;
}

//渲染最后链接
//编辑:CHANGED TO ALWAYS ALOWAY SHOW LAST LINK ATAST
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;
}

//删除双斜杠。注意:有时我们可以在倒数第二个链接中以双斜杠
//结尾,因此我们将删除所有双斜杠。
$ output = preg_replace(#([^:])// +#,\\1 /,$ output);

//添加包装HTML如果存在
$ output = $ this-> full_tag_open。$ output。$ this-> full_tag_close;

return $ output;
}


Ive trying to make a pagination with CodeIgniter, it should be so simple according the manual of Codeigniter, even in the example is like this

« First < 1 2 3 4 5 > Last »

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

in the view I only call it like this pagination->create_links(); ?> (or I send it through the view when I call it from the controller, still I only get this

1 2 3 >

and there is no way to make it look like the exmaple, may sound so dummy but , anyone could help me with this? or have a problem similar?

Thanks

EDIT 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);

I did this according some advices Ive recieve, like you said when are many data it works good, still I'd LOVE to show first and next button all time, I set total_rows after my query (which I call with the right number of rows) , and I tried before too and the results are the same, I also need to show only 4 numbers and I am ustying numb_links ... still doesnt work (I dont know why Ci docs say should work..) any idea?

Thanks!

解决方案

To produce what the example shows is actually pretty easy. You just need to extend the Pagination's Library to accommodate this. I was able to do this. No matter how many pages you show it still shows first, last, back arrow, and forward arrow.

If you are wanting to show 5 pages at all times with the foward and back stuff you need to have that many results to fill that page. Then you set num_links to what you would want before and after if on the 3rd page. So it would be 2. My changes if you are on the 1st page make it display 4 pages after when applicable. See image below. White is current page. Green is pages available.

Hopefully I've explained everything correctly and this works for you. Let me know.

Controller

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

EXTENDED PAGINATION LIBRARY CALL

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

这篇关于如何配置分页codeigniter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆