【问题标题】:CodeIgniter - How to add strings to pagination link?CodeIgniter - 如何将字符串添加到分页链接?
【发布时间】:2013-04-28 18:51:10
【问题描述】:

我想在生成的分页链接末尾添加一些字符串/值。 例如,我得到了这个

http://localhost/products/lists/5

我想拥有

http://localhost/products/lists/5/value/anothervalue

所以,我需要以某种方式发送这些值... :)

谢谢大家。

【问题讨论】:

    标签: codeigniter pagination


    【解决方案1】:

    分页类有一个名为 suffix 的未记录配置选项,您可以使用它。以下是我在其中一款应用中使用它的方式:

    // get the current url segments and remove the ones before the suffix
    // http://localhost/products/lists/5/value/anothervalue
    $args = $this->uri->segment_array();
    unset($args[1], $args[2], $args[3]);
    $args = implode('/', $args);
    
    // $args is now 'value/anothervalue'
    
    $base_url = 'http://localhost/products/lists';
    
    $this->pagination->initialize(array(
        'base_url' => $base_url,
        'suffix' => '/'.$args,
        'first_url' => $base_url.'/1/'.$args,
        'uri_segment' => 3
    ));
    

    【讨论】:

    • 那就是萨姆兹!天呐!一直在寻找 2 天,但从未见过该解决方案! :)
    【解决方案2】:

    application/config/routes.php

     $route['products/lists/(:num)/value/(:any)'] = "products/lists/$1/$2";
    

    控制器代码application/controllers/products.php

    class Products extends CI_Controller {
        public function index() {
            $this->load->view('welcome_message');
        }
    
        public function lists($page = 1, $value = null) {
            $this->load->view('product_lists', array('page' => $page, 'value' => $value));
        }
    }
    

    这样如果你的网址是http://localhost/products/lists/5/value/anothervalue 在函数 lists 中将是 $page = 5 和 $value = 'anothervalue' 并且它们将在模板 product_lists ($page, $value) 中可用

    【讨论】:

      猜你喜欢
      • 2018-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-28
      • 2014-09-13
      • 2011-06-15
      相关资源
      最近更新 更多