dongshi3361 2015-01-12 14:51
浏览 67
已采纳

在URL中传递多个参数的可能性?

I'm working on a search-based website, and am trying to pass parameters using SEO-friendly URLs.

Is it possible to pass the following URL and get the URL in CodeIgniter?

http://www.example.com/search/prize/10/13/14.5/size/xl/2xl/color/black/white-grey/

I am able to create the URL, but I want to get the URL values like $prize = array("10","13","14.5"), $size= array("xl","2xl"), and $color = array("black","white-grey").

I tried to use the uri_to_assoc() function, but it didn't work. I get the following output:

[price] => 10,
[13] => 14.5
...

which is wrong.

Note: I tried to use $this->uri->segment(1), etc., but in this case, the segment position is dynamic.

For example, users may search for only prices of $10, so the URL will get changed to:

http://www.example.com/search/prize/10/size/xl/2xl/color/black/white-grey/

Now the segment position of getting the size must be changed. In this case, I want:

$prize = array("10");
$size = array("xl", "2xl");
$color = array("black", "white-grey");

How can I achieve this?

  • 写回答

2条回答 默认 最新

  • dongna9185 2015-01-12 15:04
    关注

    You are using quite the unconventional "friendly URI" format. Normally when passing parameters there is a single identifier and then the parameter e.g. /name/key/name/key/name/key.

    When you use the correct format /name/key/name/key/name/key in conjunction with uri_to_assoc(), you would get:

    array(
        'name' => 'key',
        // etc...
    )
    

    but using something like /prize/1/2/3/size/s/m/l/color/black/white/grey would yield:

    array(
        'prize' => 1,
        2 => 3,
        'size' => 's',
        'm' => 'l',
        // etc...
    )
    

    Which is useless to you.

    You will have to fetch all of the segments individually and build your arrays with a foreach:

    $segments = $this->uri->segment_array();
    $prize = array();
    $size = array();
    $color = array();
    $curKey = '';
    foreach ($segments as $seg) {
        if (in_array($seg, array('prize', 'size', 'color'))) {
            $curKey = $seg; continue;
        }
        if (!empty($curKey)) ${$curKey}[] = $seg;
    }
    
    // use $prize, $size, and $color as you wish
    

    or alternatively using a multi-dimensional array:

    $parameters = array(
        'prize' => array(),
        'size' => array(),
        'color' => array(),
    );
    $segments = $this->uri->segment_array();
    $curKey = '';
    foreach ($segments as $seg) {
        if (in_array($seg, array('prize', 'size', 'color'))) {
            $curKey = $seg; continue;
        }
        if (!empty($curKey)) $parameters[$curKey][] = $seg;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度