2012-01-27 85 views
0

我想知道CodeIgniter是否允许页码之后的分段,以及最好的方法是做什么?在CodeIgniter的页码之后有分段

$config['base_url'] = '/controller/view/pg/';

我需要我的传呼也通过这个:

/controller/view/pg/1/v/l/rpp/20 ...等

我已经因为段数跑进多个问题,因为我使用$this->uri->uri_to_assoc(n)我会需要...

我需要能够传递值到每个页面,在这一点上我不知道该怎么做。

您是否认为最好的方法是始终将分页移动到所有其他分段的末尾?看来这也会导致问题。

+0

我相信你是错的Jakub;请参考文档http://codeigniter.com/user_guide/libraries/pagination.html(第一个例子) – jason 2012-01-27 14:43:18

+0

我认为*您提到了'config.php',它也有'$ config ['base_url']' 。在这种情况下,您使用了分页特定的配置,我将删除我的评论。 – Jakub 2012-01-27 14:58:13

+0

如果base_url为空,那么页码只显示在URL中没有别的... – jason 2012-01-27 14:58:46

回答

2

贾森,你自己只是因为你松散地跟踪了哪些区块是控制器/方法的一部分,以及哪些是你认为相关的区段。

我会首先告诉大家坚持一个方法说给其追加到结束(这是从URI,而不是你的路由配置用户的角度来看):

/view/page/1233/name/blue-skies/pg/20 

上面的格式会在后端表示如下: /view/是控制器,page是您在控制器中的方法,那么您将使用$this->uri->uri_to_assoc(4)(第4个元素,名称即可启动)。

这样你就可以正确地捕捉到你的页码1233,然后把所有相关的数据给它。

作为一个建议,我会提醒注意不要使用不可读的变量,这会导致混淆,并且不会使您的网站网址搜索引擎友好(谁知道最终是什么/ v/p/123/v/l/20? )。

如果您在使用uri路由时遇到问题,请始终使用您的$this->output->profiler(TRUE);。除非你真的需要,否则不要惹你的路由配置,这可能会导致混淆,这会使你的测试变得复杂。

编辑

我,因为根据你的问题,你可以把它解释为从config.php文件或分页类的位置的路由问题造成了混乱。我从第一种方法中拿走了它。

为了说明你需要简单地坚持一个干净的URL方法,如果你使用uri_to_assoc,那很好。但是不要忽略你的分页页码。

您可以通过该页面数在URI

末的最后一个元素解决这个问题:/view/page/1233/name/blue-skies/user/12/20

20是由分页产生的页面数,其他都段你用于任何事情。

您需要将您的$config['uri_segment'] = 6;在这种情况下,你的$config['base_url'] = '/view/page/1233/'.$this->uri->assoc_to_uri($uri_segments);

其中:

$uri_segments = array(
'name' => 'blue-skies', 
'user' => '12'); 

IF

它是未知的多少段你有(比如动态$ uri_segments数组),使用$this->uri->total_segments()来计算总分段数,然后你的分页数就是那个(最后一个)的+1。

+0

好吧,我有点迷路;你必须现在的页码?以及当最后一个值必须是页码时,我如何将剩余的数据附加到base_url ... – jason 2012-01-27 14:57:36

+0

我已更新我的答案,对不起,路由问题总是引起混淆。 – Jakub 2012-01-27 15:10:42

+0

感谢您的澄清,这使得现在更有意义... – jason 2012-01-27 16:00:42

0

是的,可以做到。

做它是在分页配置数组的方式, 'uri_segment' 应该是可变:

$配置[ 'uri_segment'] = $ segment_offset;

$ segment_offset可以通过在URI中查找'/ pg /'(在你的例子中)来计算。

示例代码:

//for pagination  
    $start = 0; 
    $limit_per_page = 100; 


    //URI to acoc array: 
    $uri_array = $this->uri->uri_to_assoc(4); 
    /* 
    Array 
     (
      [page_links] => 0 
     ) 
    */ 

    //Take the number of pagination segment from uri; return URI number by its name 
    $segment_offset = 0; 

    foreach($uri_array as $key=>$value){ 
     $segment_offset++; 
     if('page_links' == $key){ 
      //segment founded 
      break; 
     } 
    } 

    //calculate actual place or pagination number 
    //$segment_offset = $segment_offset + uri_to_assoc(**4**) + **1** place after the segmwnt 'page_links' is actual number for pagination; 
    $segment_offset = $segment_offset + 4 + 1; 


    //DB query can be here 

    // /////////////////////////////////////////////////////////////////////// 
    // NOTE: Set up the paging links. Just remove this if you don't need it, 
    // NOTE: ...but you must remember to change the views too. 
    // /////////////////////////////////////////////////////////////////////// 
    $this->load->library('pagination'); 
    $this->load->helper('url'); 


    $config['base_url']  = site_url('controller1/browse/pg/'.$pg.'/other_segment/etc..'); 
    $config['total_rows'] = xxx; 
    $config['per_page']  = $limit_per_page; 

    //$config['uri_segment'] = xx; 
    //now that can be variable, and not just on the end of the URI 
    $config['uri_segment'] = $segment_offset; 


    $config['first_url'] = '0'; 

    $config['num_links'] = 4; 

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

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