CodeIgniter 中页码后面有段

发布于 2024-12-29 21:18:35 字数 370 浏览 0 评论 0原文

我想知道 CodeIgniter 是否允许页码后面的段以及执行​​此操作的最佳方法是什么?

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

我需要我的分页也传递这个:

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

我遇到了多个问题,因为我使用 $this->uri->uri_to_assoc(n) 因为我将要使用的段数需要...

我需要能够将值传递到每个页面,目前我不知道该怎么做。

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

I am wondering if CodeIgniter allows segments after the page number and what is the best way to do this?

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

I need my paging to pass this also:

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

I have ran into multiple problems because I am using $this->uri->uri_to_assoc(n) because of the number of segments I will be needing...

I need to be able to pass values to each page, and at this point I'm not sure how to do it.

Do you think the best way to do this is to always move the paging to the end of all other segments? It seems that this leads to problems also.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不再见 2025-01-05 21:18:35

杰森,您自己制造这个问题只是因为您不知道哪些部分是控制器/方法的一部分,以及哪些部分是您认为相关的。

首先,我会告诉您坚持一种方法,即将其附加到末尾(这是从 uri 中的用户角度来看,而不是您的路由配置):

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

上面的格式在后端意味着类似这样的内容:
/view/ 是控制器,page 是控制器中的方法,那么您将使用 $this->uri->uri_to_assoc(4)< /code> (第四个元素,是开始的名称)。

这样您就可以正确捕获页码 1233 以及与其相关的所有数据。

作为建议,我会警告不要使用不可读的变量,它会导致混乱,并且不会使您的网站 url SEO 友好(谁知道 /v/p/123/v/l/20 最终意味着什么?)。

如果您在 uri 路由方面遇到问题,请始终使用您的 $this->output->profiler(TRUE);。除非确实需要,否则不要弄乱您的路由配置,这可能会导致混乱,从而使您的测试变得复杂。

编辑

我造成了混乱,因为根据您的问题,您可以将其解释为来自 config.php 文件或分页类位置的路由问题。我从第一种方法中采取了它。

为了澄清你需要简单地坚持一个干净的 url 方法,如果你使用 uri_to_assoc,那很好。但请不要忘记分页的页码。

您可以通过将页码设置为 uri 中的最后一个元素来解决此问题

Last: /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(最后一个)。

Jason, you are making the problem yourself simply because you loose track of what segements are part of the controller/method, and which are your segments that you find relevant.

I would first of all tell you to stick to one method say to append it to the end (this is from the users perspective in the uri, not your route config):

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

The above format would mean something like this on the backend:
/view/ is the controller, page is your method in the controller, then you would use $this->uri->uri_to_assoc(4) (4th element, being name to start).

That way you correctly capture your page number 1233 and then all relevant data to it.

As a suggestion, I would caution against using unreadable variables, it leads to confusion and does NOT make your site url SEO friendly (who knows what /v/p/123/v/l/20 means in the end?).

Always user your $this->output->profiler(TRUE);, if you are having trouble with uri routing. Don't mess with your route config unless you REALLY need to, this may lead to confusion, which complicates your testing.

EDIT

I created confusion because based on your question you could interpret it as a routing issue from the position of the config.php file OR the pagination class. I took it from the first approach.

To clarify you need to simply stick to a clean url method, if you use uri_to_assoc, thats fine. But just don't loose track of your page number for the pagination.

You can solve this by making the page number the last element in your uri

Last: /view/page/1233/name/blue-skies/user/12/20

Where the 20 is the page number that is generated by pagination, the other are segments you use for whatever.

You would set your $config['uri_segment'] = 6; in this case, and your $config['base_url'] = '/view/page/1233/'.$this->uri->assoc_to_uri($uri_segments);

Where:

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

IF

It is unknown how many segments you have (say a dynamic $uri_segments array), use $this->uri->total_segments() to count total segments, then your pagination one is the +1 to that (last).

天涯离梦残月幽梦 2025-01-05 21:18:35

是的,这是可以做到的。

方法是在分页配置数组中,'uri_segment'应该是变量:

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

Yes, that can be done.

The way to do it is in pagination configuration array, 'uri_segment' should be variable:

$config['uri_segment'] = $segment_offset;

$segment_offset can be calculated by looking for '/pg/'(from your example) in the URI.

Example code:

  //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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文