-
Notifications
You must be signed in to change notification settings - Fork 7.6k
URI helper
URI helper makes it simpler to get & set associative arrays in the URI.
I got tired of writing multiple lines of code each time I used the URI library for getting and setting URI arrays. I don't like to pull data from segments by numbers because it limits my flexibility in the way that I call pages & pass variables. I'd much rather pass name/value pairs the URIs than as data in numbered segments.
I usually set this helper to autoload in the config so that it's always available. I often pass & retrieve success messages & similar simple messages with this. It only cuts down on a few lines of code, but makes me a lot more excited about using URIs to pass name/variable pairs rather than using segments. . Save this to your helper folder with whatever name you'd like to use. I personally use 'uri_message_helper' Like any helper, when you call it to load use something like the following: $this->load->helper('uri_message');
[code] <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); // this helper simply gets & sets associative URI name/variable pairs
/* returns the content of $var from the URI. Optional $pos parameter lets you set the URI string starting point. Example: $id=get_uri('news_id'); would return value of 10 from URI the following URI; /index.php/admin/index/news_id/10 / function get_uri($var,$pos=3){ $CI =& get_instance(); $array=$CI->uri->uri_to_assoc($pos); if (isset($array[$var])){ return $array[$var]; } else { return false; } } / sets one name/variable pair to the URI string. Handy for passing simple messages without any other parameters in the URI. $str=set_uri("varname1","variable"); would output varname1/variable to the uri string / function set_uri($name,$var){ $CI =& get_instance(); $array = array($name => $var); $str = $CI->uri->assoc_to_uri($array); return $str; } / allows you to pass an array of values into the URI string.Example: $array=array('item1'=>'item1info', 'item2' =>'item2info', 'item3' =>'item3info', ); $str=set_uri_array($array); redirect('admin/index/'.$str,'location'); */ function set_uri_array($user_array){ $CI =& get_instance(); $array=array(); foreach ($user_array as $item => $key ){ $array[$item]=$key; } $str = $CI->uri->assoc_to_uri($array); return $str; }
?> [/code]