CodeIgniter Forums

Full Version: Passing an array through GET while using url_to_assoc
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]jhyland[/eluser]
Currently, on the website I have now (which will be replaced with the new CI site), there are multi selects and check boxes. So if someone selects two check boxes, or two options in a multi select, the URL looks like this..

http://website.com/?checkbox[]=var1&chec...ti;[]=var2

(Ignore the ';', the forum keeps putting those in there.)

That will get processed as this:

array(
checkbox => array('var1','var2'),
multi => array('var1','var2')
);

I want to use the $this->uri->uri_to_assoc option in CI, but that only does a single level associative array.

How can I accomplish the same task, using the url_to_assoc?

Also, how do I have a CI form "action" go to that type of url? Meaning, instead of it going to ?this=that, how do I make it go to /this/that/ ?

Thanks!

El Forum

[eluser]InsiteFX[/eluser]
json encode and json decode

El Forum

[eluser]jhyland[/eluser]
How do I have the form post the json code to a URL? And how does CI interpret /something/like/this/ as json?

El Forum

[eluser]InsiteFX[/eluser]
You need to use javascript.

jQuery - Read up Ajax

El Forum

[eluser]jhyland[/eluser]
You just gave a link to the jquery site... Can you help me out with maybe something more specific?

El Forum

[eluser]PhilTem[/eluser]
Just make your forms submit GET data and no POST data.

Code:
echo form_open($action, array('method' => 'GET'));

Voila Wink

El Forum

[eluser]jhyland[/eluser]
Except, the point is to get rid of the ugly url: http://website.com/?checkbox[]=var1&chec...ti;[]=var2

El Forum

[eluser]jhyland[/eluser]
This works. I modified the URI.php file.

Code:
function _uri_to_assoc($n = 3, $default = array(), $which = 'segment')
{
  if ($which == 'segment')
  {
   $total_segments = 'total_segments';
   $segment_array = 'segment_array';
  }
  else
  {
   $total_segments = 'total_rsegments';
   $segment_array = 'rsegment_array';
  }

  if ( ! is_numeric($n))
  {
   return $default;
  }

  if (isset($this->keyval[$n]))
  {
   return $this->keyval[$n];
  }

  if ($this->$total_segments() < $n)
  {
   if (count($default) == 0)
   {
    return array();
   }

   $retval = array();
   foreach ($default as $val)
   {
    $retval[$val] = FALSE;
   }
   return $retval;
  }

  $segments = array_slice($this->$segment_array(), ($n - 1));

  $var = array_chunk($segments, 2);

  $result = array();
  foreach ($var as $eachSet) {
   if (isset($eachSet[0]) and isset($eachSet[1])) {
    if (isset($result[$eachSet[0]])) {
     if (is_array($result[$eachSet[0]])) {
      $result[$eachSet[0]][] = $eachSet[1];
     } else {
      $result[$eachSet[0]] = array($result[$eachSet[0]]);
      $result[$eachSet[0]][] = $eachSet[1];
     }
    } else {
     $result[$eachSet[0]] = $eachSet[1];
    }
   }
  }

  // Cache the array for reuse
  $this->keyval[$n] = $result;
  return $result;
}

The request: http://localhost/index.php/ci/view_assets/foo/a/bar/b/foo/c/bar/d/test/true

Returns:
Quote:Array
(
[foo] => Array
(
[0] => a
[1] => c
)

[bar] => Array
(
[0] => b
[1] => d
)

[test] => true
)

Hope this helps other people

El Forum

[eluser]jhyland[/eluser]
This would also entail editing the assoc_to_uri function for redirecting to the uri.

Code:
function assoc_to_uri($array)
{
  $temp = array();
  foreach ($array as $key => $val)
  {
   if(is_array($val))
   {
    foreach($val as $subkey => $subval)
    {
     $temp[] = $key;
     $temp[] = $subval;
    }
   }
   else
   {
    $temp[] = $key;
    $temp[] = $val;
   }
  }
  return implode('/', $temp);
}

El Forum

[eluser]jhyland[/eluser]
http://www.linuxdigest.org/2012/08/codei...y-parsing/


There ya go Smile