-2
//let my controller be C and method be:
    function X($array){}
//And my url to call it is:
    localhost/mysite/C/X/array

Well i tried it but it returns 400-Bad Request response. Does anyone know how to do it? Quick response will help me a lot//Thanx

edam
  • 910
  • 10
  • 29

6 Answers6

2

localhost/mysite/C/X/?array=1&&?array=2....

$array = $this->input->get('array');

or

localhost/mysite/C/X/1,2,3,4,5

$array = explode(',' $this->uri->segment(n));

// in app/config/config.php

$config['permitted_uri_chars'] = 'a-z 0-9~%.:,_-';

My variant for array in url (/tours/novogodniye-turi/visa=yes;duration=small;transport=4,7,6,2/)

if ( ! function_exists('filter_parse_segment'))
{
    function filter_parse_segment($segment, $merge_to_get = FALSE)
    {   
        if(empty($segment))
        {
            return FALSE;
        }

        $parameters = explode(";", (string)$segment);

        if(empty($parameters))
        {
            return FALSE;
        }

        $parameters_array = array();

        foreach($parameters as $parameter)
        {
            if(empty($parameter))
            {
                continue;
            }

            $parameter = explode("=", $parameter);

            if( ! isset($parameter[0], $parameter[1]) or empty($parameter[0]))
            {   
                continue;
            }

            if(strpos($parameter[1], ','))
            {
                $parameter[1] = explode(",", $parameter[1]);
            }

            $parameters_array[$parameter[0]] =  $parameter[1];      
        }

        if($merge_to_get === TRUE)
        {
            $_GET = array_merge($_GET, $parameters_array);
        }

        return $parameters_array;
    }
}

// --------------------------------------------------------------------

if ( ! function_exists('filter_collect_segment'))
{
    function filter_collect_segment($array, $suffix = '', $remove = array())
    {   
        if(empty($array) || ! is_array($array))
        {
            return '';
        }

        $segment_str = '';

        foreach ($array as $key => $value)
        {

            if(empty($key) || in_array($key, (array)$remove))
            {
                continue;
            }

            if( ! $segment_str == '')
            {
                $segment_str = $segment_str.';';
            }

            if( ! is_array($value))
            {
                $segment_str = $segment_str.$key.'='.$value;

                continue;
            }

            if(empty($value))
            {
                continue;
            }

            $parsed_value = '';

            foreach ($value as $item)
            {
                if( ! $parsed_value == '')
                {
                    $parsed_value = $parsed_value.',';
                }

                if(is_array($item) || empty($item))
                {
                    continue;
                }

                $parsed_value = $parsed_value.$item;
            }

            $segment_str = $segment_str.$key.'='.$parsed_value;
        }

        if($segment_str != '')
        {
            $segment_str = $segment_str.$suffix;
        }

        return $segment_str;
    }
}


// --------------------------------------------------------------------

if ( ! function_exists('filter_key'))
{
    function filter_key($filter_array, $key, $value = NULL)
    {   
        if( ! isset($filter_array[$key]))
        {
            return;
        }

        if($value == NULL)
        {
            return $filter_array[$key];
        }

        if( ! is_array($filter_array[$key]) && $filter_array[$key] == (string)$value)
        {
            return $value;
        }

        if(is_array($filter_array[$key]) && in_array($value, $filter_array[$key]))
        {
            return $value;
        }

        return FALSE;
    }
}
Oleg Ozimok
  • 259
  • 2
  • 4
  • That could be a way..If i make it a string with _(underscore) then I can split it to an array in my controller..I will try it..You've got the point. Thanx a lot – edam Dec 10 '13 at 13:44
  • underscore version - $array = explode('_' $this->uri->segment(n)); – Oleg Ozimok Dec 10 '13 at 17:42
1

If you want the solution in the pretty nice URL then you have to loop the array first and then concatenate the elements with some - dash or + plus signs like this;

$array = array(1,2,3,4);
$string = "";

foreach($array as $value){
   $string .= $value."-";
}

$string = rtrim($string, "-");

redirect(base_url()."get_products?param=".$string);

And on the next page just get the param and use explode() function with - dash sign to create the array again.

Tahir Afridi
  • 190
  • 3
  • 14
1

From your route

$routes->add('/your-url/(:any)', 'Your-controller::YourFunction');

and on your function

 YourFunction($segment = null){

  if(!$segment)

      return redirect()->to(base_url());

  }else{

      //on this condition you can separate the url using the the 

      //getSegment() by choosing the index or position from the url

      $segment= $this->request->uri->getSegment(2);

  }
 }

Hope it helps! thanks!

XRT-NoOne
  • 46
  • 8
0

try your url like this

 if value you have to pass is
 [1,2,3,2]
 then
 localhost/mysite/index.php/C/X/1,2,3,2
rajesh kakawat
  • 10,826
  • 1
  • 21
  • 40
0

In a simple way you can make the array a string with some special character in between the values of the array.

Then in the landing page you can split the string with the special character and get the array again.

If the values are [1,2,3,4], then make it using a loop "1,2,3,4".

Then pass the string.

In teh landing page split the string with "," and you will again get the array.

Hope it helps you.

Thanks

Gourav
  • 813
  • 2
  • 10
  • 23
0

Why don't you use uri segments for array? You can count uri segments and could use them. Even u could check how many arrays already there.

url.com/1/2/3

http://ellislab.com/codeigniter/user-guide/libraries/uri.html

Note: uri segment doesnt work on main controller index function, you have to define another function than index for example: I don't know what it happens but i think because of htaccess file I'm using do remove index.php.

url.com/uri_segment_controller/go/1/2/3

Mehmet Uyarovic
  • 304
  • 2
  • 9