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

2 Answers 2

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"); 
?> 
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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");

7 Comments

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
I think I understand. Then what sets those parameters ? is it an action made by your user somewhere ?
yes it's just like seoclerks.com take a look at it . they are using javascript for display view i guess
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
ajax you mean to make all three views type and than hide two
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.