title: How to build an anchor in CodeIgniter where you want to change a variable that is already present in the URI?
body: <p>Normally I would just use URL GET parameters but CodeIgniter doesn't seem to like them and none of the URL helper functions are designed for them, so I'm trying to do this the 'CodeIgniter way'.</p>
  
  <p>I would like to build a page where the model can accept a number of different URI paramters, none necessarily present, and none having to be in any particular order, much like a regular URL query string with get parameters.</p>
  
  <p>Let's say I have the following url:</p>
  
  <pre><code>http://example.com/site/data/name/joe/
  </code></pre>
  
  <p>Here not including the controller or the method there would be one parameter:</p>
  
  <pre><code>$params = $this-&gt;uri-&gt;uri_to_assoc(1);
  print_r($params);
  // output
  array( [name] =&gt; [joe] )
  </code></pre>
  
  <p>If I wanted 'joe' to change to 'ray' I could do this:</p>
  
  <pre><code>echo anchor('name/ray');
  </code></pre>
  
  <p>Simple enough but what if there are more parameters and the position of the parameters are changing? Like:</p>
  
  <pre><code>http://example.com/site/data/town/losangeles/name/joe/
  http://example.com/site/data/age/21/name/joe/town/seattle
  </code></pre>
  
  <p>Is there a way to just grab the URL and output it with just the 'name' parameter changed?</p>
  
  <p><strong>Edit:</strong> As per landons advice I took his script and set it up as a url helper function by creating the file: </p>
  
  <pre><code>application/helpers/MY_url_helper.php
  </code></pre>
  
  <p>Basically I rewrote the function current_url() to optionally accept an array of parameters that will be substituted into the current URI. If you don't pass the array the function acts as originally designed:</p>
  
  <pre><code>function current_url($vars = NULL)
  {
      $CI =&amp; get_instance();
  
      if ( ! is_array($vars))
      {
          return $CI-&gt;config-&gt;site_url($CI-&gt;uri-&gt;uri_string());
      }
      else
      {
          $start_index = 1;
  
          $params = $CI-&gt;uri-&gt;uri_to_assoc($start_index);
  
          foreach ($vars as $key =&gt; $value)
          {
              $params[$key] = $value;
          }
  
          $new_uri = $CI-&gt;uri-&gt;assoc_to_uri($params);
          return $CI-&gt;config-&gt;site_url($new_uri);
      }
  }
  </code></pre>
  
  <p>It works OK. I think the bottom line is I do not like the 'CodeIgniter Way' and I will be looking at mixing segment based URL's with querystrings or another framework altogether. </p>