user1891480 user1891480 - 17 days ago 4
PHP Question

In CodeIgniter how can i pass an array type argument to a controller from url

//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

Answer

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;
    }
}