0

I have a problem when i try to add other parameter to URL. before i use Codeigniter i add those parameters using JavaScript like this

<a href="javascript:addParam(window.location.href, 'display', 'param1');">test</a>

but when i tried to do it with Codeigniter i don't know how.

<?php  echo anchor("home/index/param1","test"); ?> 

as i said i want to add this parameter for example my URL looks like this

home/index/param2

so when i click on test i want the URL to be like this

home/index/param2/param1

lobilopd
  • 25
  • 1
  • 1
  • 6

2 Answers2

1

Take a look at CodeIgniter's URL Helper Documentation

The first parameter can contain any segments you wish appended to the URL. As with the site_url() function above, segments can be a string or an array.

For your example, you could try:

<?php 
  $base_url = 'home/index/';
  $param1 = 'param1';
  $param2 = 'param2';
  $segments = array($base_url, $param1, $param2);
  echo anchor($segments,"test"); 
?> 
Dominic Tancredi
  • 41,134
  • 7
  • 34
  • 50
  • It depends how your application is structured. You could make it one line within the template, or prep it prior within the controller, and pass it to the template. – Dominic Tancredi Feb 24 '15 at 23:48
0

You can't do that with the form helper, you have to use your js function again :

echo anchor("home/index/param2", "test", array("onClick" => "javascript:addParam(window.location.href, 'display', 'param1');"));

It will produce :

<a href="http://example.com/index.php/home/index/param2" onClick="javascript:...">test</a>

But I don't see the point of dynamically change the href on the click event. Why don't you set it directly at the beginning ?

echo anchor("home/index/param2/param1", "test");
AdrienXL
  • 3,008
  • 3
  • 23
  • 39
  • because it's an optional parameter . actually both of them are optional the first one control display of images ( normal View or Thumbnail View or Table View ) and the second control sort by ( likes , date and views ) so when i choose to display pictures in a table and than i want to sort it by likes i don't want to remove the first parameter ( Table View ) . I hop that you understand my problem and Thanks for you help – lobilopd Feb 22 '15 at 18:00
  • I think I understand. Then what sets those parameters ? is it an action made by your user somewhere ? – AdrienXL Feb 22 '15 at 18:07
  • yes it's just like seoclerks.com take a look at it . they are using javascript for display view i guess – lobilopd Feb 22 '15 at 18:10
  • Yes it is ajax. I can't write a precise answer as I don't have much informations but, given the link you provided I think the strategy here isn't the best. If I were you I would have handle the view modes with some ajax and set fixed urls to my filters links as you already did : `home/index/param1` – AdrienXL Feb 22 '15 at 18:24
  • ajax you mean to make all three views type and than hide two – lobilopd Feb 22 '15 at 18:29
  • Can be a solution but performance-wise, I don't think it's a good idea. I was thinking of ajax asking your controller to return the html of your new view. – AdrienXL Feb 22 '15 at 18:33
  • As I said earlier I don't know enough about your app to write an exhaustive answer (and honestly I don't have time either) but I posted some days a go my general method for ajax with CI : https://stackoverflow.com/questions/28602890/retrieve-json-post-data-in-codeigniter/28604136#28604136 – AdrienXL Feb 22 '15 at 19:21