简体   繁体   中英

how to pass a (“/”) through href

I am passing an id containing / eg: 171/CR/EOW1/14 in the link.

It is showing correctly, but in the controller function it is taking only the first letters before the slash. eg: 171

How do I solve this problem?

在此处输入图片说明

Your question is incredibly vague. But for the purposes of this, I'll assume that you want to pass the whole string 171/CR/EOW1/14 . Not parts of the string as different params.

you are using an un-escaped slash. So codeigniters' routing thinks the parts of the url after the 171 are more parameters in the route string.

if you want to pass a url, use urlencode() and then urldecode() to handle the slashes in the string you want to pass.

Or use addslashes() .

addslahes()

urlencode()

You can use uri_segment , which should help.

http://example.com/index.php/controller/action/1stsegment/2ndsegment

it will return

$this->uri->segment(1); // controller
$this->uri->segment(2); // action
$this->uri->segment(3); // 1stsegment
$this->uri->segment(4); // 2ndsegment

In PHP 5.6 you can retrieve as a variable argument list which can be specified with the ... (spread) operator

function do_something($first, ...$all_the_others)
{
    var_dump($first);
    var_dump($all_the_others);
}

or if you are using a lesser version you have to specify separate arguments variables

function do_something($first, $second, $third)
{
    var_dump($first);
    var_dump($second);
    var_dump($third);
}

EDIT:

You can route the url to this function like

$route['products/(:any)'] = 'catalog/do_something';

Please check the documentation for more details about url routing

Passing URI Segments to your methods in codeigniter visit codeIgniter docs

If your URI contains more than two segments they will be passed to your method as parameters.

For example, let's say you have a URI like this:

example.com/index.php/products/shoes/sandals/123

Your method will be passed URI segments 3 and 4 (“sandals” and “123”):

<?php
class Products extends CI_Controller {

        public function shoes($sandals, $id)
        {
                echo $sandals;
                echo $id;
        }
}

You can you use a question mark like:

?first=171&second=CR

For more information, see eg http://html.net/tutorials/php/lesson10.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM