Welcome Guest, Not a member yet? Register   Sign In
url helper: anchor() with array param losing first assoc key?
#1

[eluser]Unknown[/eluser]
Running PHP 532 and CI 172:

Test code in a controller function:

Code:
$this->load->helper('url');
$assoc_uri = $this->uri->uri_to_assoc(1); // get full uri
$test_link = anchor($assoc_uri, 'test link'); // bugged? skips the first key?
$test_link2 = anchor($this->uri->assoc_to_uri($assoc_uri), 'test link 2');

The first link is bugged. It loses the name of the controller in the final URI. Compare that with the second use of anchor (string), using the same data, that works as expected. For example:

test_link = http://localhost/testsite/about (broken)
test_link2 = http://localhost/testsite/welcome/about (works)

Is this a known issue? Am I missing something glaringly obvious here?

cheers,
// SM
#2

[eluser]Dennis Rasmussen[/eluser]
That's because uri_to_assoc converts your URI to an array looking like this:

Code:
$array = (
  'testsite' => 'welcome',
  'about' => false
);

And when you pass that into anchor (which then sends it to site_url as it's an array) it will only use the keys of the array ('testsite', 'about') giving you the result:

test_link = http://localhost/testsite/about (broken)


---
Is there any specific reason as to why you're using uri_to_assoc?
If you want to split your URI into an array to be able to edit the array/URI then you have to convert it back into an URI from assoc (as you do in the 2nd example).
#3

[eluser]Dennis Rasmussen[/eluser]
That's because uri_to_assoc converts your URI to an array looking like this:

Code:
$array = (
  'testsite' => 'welcome',
  'about' => false
);

And when you pass that into anchor (which then sends it to site_url as it's an array) it will only use the keys of the array ('testsite', 'about') giving you the result:

test_link = http://localhost/testsite/about (broken)


---
Is there any specific reason as to why you're using uri_to_assoc?
If you want to split your URI into an array to be able to edit the array/URI then you have to convert it back into an URI from assoc (as you do in the 2nd example).
#4

[eluser]Unknown[/eluser]
Ah thanks - it's expecting a single-dimensional array, I assumed an assoc array. My bad.

The overarching goal here is to grab the current URI, manipulate select querystring params etc, then render an updated URI as a link.
#5

[eluser]Dennis Rasmussen[/eluser]
You're welcome Smile
You can still do what you want to do with an assoc. array, but you have to convert it back into an URI again.




Theme © iAndrew 2016 - Forum software by © MyBB