Skip to content
Derek Jones edited this page Jul 5, 2012 · 12 revisions

Category:Helper::Community | Category:Helper::URI 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 to 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_helper' Like any helper, when you call it to load use something like the following: $this->load->helper('uri_message');

<?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. 
* Useful if your CI install is in a subfolder
* Example Usage: 
* $id=get_uri('news_id');
* would return value of 10 from the following URI: 
* http://yoursite.com/index.php/admin/index/news_id/10
*/
if ( ! function_exists('get_uri'))
{
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. 
* Example: 
* $str=set_uri("varname1","variable");
* would output varname1/variable to your string
*/
if ( ! function_exists('set_uri'))
{
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');
*/
if ( ! function_exists('set_uri_array'))
{
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;
}
}
?>

Xwero in the forums suggested that I combine the set_uri_array & the set_uri into one function. Personally, I like keeping them separate just because at a glance It's easier for me to identify what I'm trying to do when they're kept separate. That being said, if you'd prefer one function for both you could use this (untested) code that's based on his comments;

if ( ! function_exists('set_uri'))
{
function set_uri($data,$value=null)
{
  $CI =& get_instance();
  $array = array();
  if(is_array($data))
  {
    foreach ($data as $key => $val )
    {
      $array[$key]=$val;
    }
  }
  elseif(is_string($data) && $value !== null)
  {
    $array[$data]=$value;
  }
  else
  {
    return false;
  }
  return $CI->uri->assoc_to_uri($array);
}
}
Clone this wiki locally