0

I am trying to use the current_url() function in codeigniter but not sure how to manipulate this data. I understand I can echo out the current_url() but how do I take each of the segments and use to generate a new URL?

I am trying to take pieces of the current page and use to generate a new URL with adding in new variable data.

I have never worked with current_url(); before, so not sure what to do. Lets say my user is on a filters page and has 2 variables to filter their results (neighborhood and category). If the user clicks on a neighborhood, how would I structure the hyperlink to pull the current url variable for the category and then insert the variable for neighborhood?

My URLs look something like: mydomain.com/ny/find/$neighborhood/$category

   Breakdown:
   ny = controller
   find = function
   param1 = $neighborhood
   param2 = $category

Any help would be much appreciated...

user2763616
  • 21
  • 1
  • 8
  • use $base_url instead, and just always add $neighborhood/$category when your controller directs to itself – Rooster Jul 18 '14 at 14:56

3 Answers3

0

You can breakdown your URL by means of this:

For ny: echo $this->uri->segment(1);
For find: echo $this->uri->segment(2);
For param1: echo $this->uri->segment(3);
For param2: echo $this->uri->segment(4);
RNK
  • 5,582
  • 11
  • 65
  • 133
  • i know how to pull the segements from the current URL, the issue i am having is how to integrate a 2nd param (that is not within the current URL). Meaning, lets say there are (2) parameters: neighborhood and category. I want to take 1 param from the URL and the other added based on the filter option the user will select...does that make sense? – user2763616 Jul 18 '14 at 15:19
0
<?php //check if there is a value in uri segment 3 (param1)

if($this->uri->segment(3)){ 

//if there is a value there then include it in the link ?>
     <a href="controller/function/<?=$this->uri->segment(3)?>/link">Link text</a>

<?php //otherwise just go to the link for param1

}else{ ?>

   <a href="controller/function/link">Link text</a>

<?php } ?>

This should work though depending on the exact logic needed you may have to tweak the links and also the logic in the else statements. Depending on how you want to link you may also have to add an elseif($this->uri->segment(4)) and use different logic if the user is on a page with 4 url segments.

Dan
  • 9,391
  • 5
  • 41
  • 73
  • so this would pull the current pages segement(3) and then i can add in the 2nd parameter for the other variable? – user2763616 Jul 18 '14 at 15:18
  • is there any reason to use the current_url() function or just use uri->segment(); function for that pages URL? – user2763616 Jul 18 '14 at 15:20
  • If you're on a page where the `current_url()` has both `param1` and `param2` (ie it has 4 url segments) then you will run into issues when using `current_url()` to create links. By the way you can check how many url segments are present with `$this->uri->total_rsegments()` – Dan Jul 18 '14 at 15:22
0

current_url() returns a string for the actual page loaded. You can create any url you want using the segments. Let´s say you have this current url:

mydomain.com/ny/find/$neighborhood/$category

if you want add something to this url and create a link to the user, you can make a string:

$url = base_url().$this->uri->segment(1).'/'.$this->uri->segment(2).'/newParamYouAdd/'.$this->uri->segment(4);

And then:

<a href="<?php echo $url; ?>">New url</a>

Let me know if is this what you want.

Arthur Mastropietro
  • 673
  • 1
  • 7
  • 22