【问题标题】:How to change the href of <a> tag in CodeIgniter pagination如何在 CodeIgniter 分页中更改 <a> 标签的 href
【发布时间】:2013-02-06 11:16:45
【问题描述】:

我需要在 CodeIgniter 分页中更改 &lt;a&gt; 标签的 href。 CodeIgniter 的$this-&gt;pagination-&gt;create_links(); 函数创建如下链接:

<a href="http://example.com/index.php/admin/view/3">3</a>

但是,我需要在所有段的末尾添加页码 (3)。 像这样:

<a href="http://example.com/index.php/admin/view/field/created/3">3</a>
<a href="http://example.com/index.php/admin/view/field/created/order/asc/3">3</a>

我该怎么做?

【问题讨论】:

    标签: php codeigniter pagination


    【解决方案1】:

    您需要将 URL 作为配置变量传递:

    $config = array('base_url' => site_url('/admin/view/field/created'));
    $this->pagination->initialize($config);
    $this->pagination->create_links();
    

    有关更多配置可能性,请参阅documentation。

    【讨论】:

    • 这如何回答他关于浮动分页段的问题?
    • @giraff: 网址不固定,段可以改,每次都想要最后的页码……
    【解决方案2】:

    我也遇到过。最简单的解决方案?重新配置您的 URL 结构,以便分页始终是第一段。为了不弄乱排序字段,只需确保页面始终加载分页,即当它第一次加载时,在该段中放置一个 0。除了更改设置以使用查询字符串之外,我认为您无法轻松移动分页段的位置,我想您可以在配置中执行一些 if 逻辑,例如:

    if(is_numeric($this->uri->segment(2)
    {
        $config['uri_segment'] = 2;
    } else if (is_numeric($this->uri->segment(3) {
        $config['uri_segment'] = 3;
    }
    

    但老实说,这可能会变得非常丑陋,具体取决于您要添加多少额外的细分。

    【讨论】:

      【解决方案3】:

      你可以尝试这样计算“页码”和“base url”的uri段:

      // Parsing the URI into an associative array
      $uri = $this->uri->uri_to_assoc(4);
      
      $segments = count($uri);
      
      // Calculate the uri segment and base url
      if ($segments > 1) {
         $uri_segment  = 3 + $segments - 1; // 3 segments "index.php/admin/view/ + SORT_SEGMENTS - PAGE_SEGMENT
      
         array_pop($uri); // Pop the page number
         $base_url     = site_url('admin/view/'. implode('/', $uri));
      } else {
         $uri_segment  = 4;
         $base_url     = site_url('admin/view/');
      }
      
      // Pagination config
      $config['base_url']         = $base_url;
      $config['uri_segment']      = $uri_segment;
      $config['use_page_numbers'] = TRUE;
      $config['total_rows']       = YOUR_CONFIG;
      $config['num_links']        = YOUR_CONFIG;
      $config['per_page']         = YOUR_CONFIG; 
      
      $this->pagination->initialize($config); 
      
      $pagination = $this->pagination->create_links();
      

      PD:我是阿根廷人,我的英语水平有点差

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-02-01
        • 2019-04-11
        • 2011-10-30
        • 2011-05-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多