I needed the uri_to_assoc method to be able to take an associative array that had multiple values in the array elements..
Replace the _uri_to_assoc method in
system/core/URI.php
PHP 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;
}
Example usage:
PHP Code:
$data = $this->uri->uri_to_assoc(n);
print_r($data);
If the URI was
/name/amy/name/smith/state/california/state/ohio, the result would be:
Quote:Array
(
[name] => Array
(
[0] => amy
[1] => smith
)
[state] => Array
(
[0] => california
[1] => ohio
)
)
And the other way around (array to URI)
Replace the assoc_to_uri method in
system/core/URI.php
Code:
function assoc_to_uri($array)
{
$temp = array();
foreach ($array as $key => $val)
{
if(is_array($val))
{
foreach($val as $subval)
{
$temp[] = $key;
$temp[] = $subval;
}
}
else
{
$temp[] = $key;
$temp[] = $val;
}
}
return implode('/', $temp);
}
Example usage:
Code:
$arr = array('names' => array('betsy','paul'), 'states' => array('arizona','texas'));
echo $this->uri->assoc_to_uri($arr);
Result:
Quote:names/betsy/names/paul/states/arizona/states/texas
Also, I thought it would be useful if RIGHT from the form, you could go directly to a URI that was compatible with uri_to_assoc, so I created this jquery..
Add this jquery code wherever you want, just have it load when your web page loads
Code:
form = {
/* URI Form method logic
* If the forms method="URI", then all of the inputs will be processed and a
* URI string will be constructed and appended to the end of the forms action
* location, then redirects the browser to the constructed result.
*/
uri_method: function () {
$( "form[method='URI']" ).submit( function ( e ) {
e.preventDefault();
// If this is set to '' (empty), then the empty variable will bebignored
var empty_values = '';
// All values & variables will get appended to this
var url_string = '/';
// The url_string gets appended to the form action, then redirected
var form_action = $( this ).attr( 'action' );
$( this ).find( ':input' ).each( function () {
// Skip un-named inputs (Like submit buttons)
if ( this.name === '' )
return true;
var input_name = this.name;
// If the value is empty, use the empty_values value, or ignore it
if ( $( this ).val() === '' ) {
if ( empty_values === '' ) {
return true;
} else {
url_string += input_name + '/' + url.encode( empty_values ) + '/';
return true;
}
}
var type = $( this ).attr( 'type' );
// Process radio inputs
if ( type == 'radio' ) {
$( this ).each( function () {
if ( $( this ).is( ':checked' ) ) {
url_string += input_name + '/' + url.encode( $( this ).val() ) + '/';
}
} );
}
// Checkbox inputs
else if ( type == 'checkbox' ) {
$( this ).each( function () {
if ( $( this ).is( ':checked' ) ) {
url_string += input_name + '/' + url.encode( $( this ).val() ) + '/';
}
} );
}
// Multi select fields are tricky
else if ( typeof $( this ).attr( 'multiple' ) !== 'undefined' && $( this ).attr( 'multiple' ) !== false ) {
if ( $( this ).val() ) {
$.each( $( this ).val(), function ( index, value ) {
url_string += input_name + '/' + url.encode( value ) + '/';
} );
}
}
// Any other inputs, just append them
else {
url_string += input_name + '/' + url.encode( $( this ).val() ) + '/';
}
return true;
} );
// Remove any trailing slash to avoid a double shash in the result URI
if ( form_action.substr( - 1 ) == '/' ) {
form_action = form_action.substring( 0, form_action.length - 1 );
}
var target = form_action + url_string;
//console.debug('Finished URI: ' + target);
//window.location.replace(target);
window.location.href = target;
} );
}
}
As long as the form method="URI", then it will redirect to the proper URI, this includes multiselects with multiple values and checkboxes