【问题标题】:Codeigniter editing uri segment for pagination purposesCodeigniter 编辑 uri 段以进行分页
【发布时间】:2019-10-18 17:50:48
【问题描述】:

我有一个网址http://localhost/basic/Logiciel/ab/current_page/10/date_from/1571691600000/date_to/1572382800000/time_from/30/time_to/60

我正在获取当前网址和下一页,例如:

$cu = current_url();
$current_page = $this->input->get('current_page'); 
$next_page = (int)$current_page + 1;

我想编辑$current_page 并将其设置为我选择的变量。 uri 或输入类中是否有一个函数可以让我更改 uri 段以形成新的 url,或者我必须 parse_url 并将我的 url 分解成一个数组,就像我不使用 codeigniter 时那样?。

$parse_cu = parse_url($url, PHP_URL_QUERY);
parse_str($parse_cu, $cu_arr);
$cu_arr['current_page'];

【问题讨论】:

标签: php codeigniter


【解决方案1】:

使用CI URI class,您可以使用:

吸气剂:

$arr=($this->uri->uri_to_assoc());

哪个输出

Array
(
    [current_page] => 10
    [date_from] => 1571691600000
    [date_to] => 1572382800000
    [time_from] => 30
    [time_to] => 60
)

所以获取'current_page':

    $arr=$this->uri->uri_to_assoc();
    $cp=$arr['current_page'];

并构建新的 uri 字符串:

    $arr1=$arr;
    $arr1['current_page']=$cp+1;
    echo $this->uri->assoc_to_uri($arr1);

(其中 +1 可以代表任何 +$var),输出:

current_page/11/date_from/1571691600000/date_to/1572382800000/time_from/30/time_to/60

【讨论】:

    【解决方案2】:

    这里必须使用 CodeIgniter 的 URI 类。

    URI 类有几个方法,例如 segment()uri_to_assoc()assoc_to_uri,它们可以让您完全按照自己的意愿行事。

    https://codeigniter.com/user_guide/libraries/uri.html

    【讨论】:

    • 将 url 转换为数组是简单的部分。如何生成带有变量集的 url:?
    • 您会看到如何使用称为assoc_to_uri() 的方法。您可以使用更新关联数组中的 uri 部分的方法,然后使用该方法生成最终的 uri。
    猜你喜欢
    • 2012-06-07
    • 1970-01-01
    • 1970-01-01
    • 2011-03-02
    • 2012-05-17
    • 1970-01-01
    • 2012-11-26
    • 2020-10-27
    • 1970-01-01
    相关资源
    最近更新 更多