URI Get query issues - Solve it once for all |
[eluser]damienwjking[/eluser]
Hi all, I have been going round and round trying different theorys trying to solve get queries. Some involve config changes, rewrites, modifing CI URI and extending it with a custom class. Can someone please show the newbies the correct way to do this. Please I want to do this www.test.com/controller/function/?title=Damiens %Test&url=http://www.test2.com/help/codeigniter.html In theory it should be simple but I have spent more time tying to solve this than I have on the entire build of the site. I've tried lots of differnt code from different forum posts but I always get nothing returned. Is the a simple recommended way of doing this? Rick if you are listening, can you please get this documented as it has been a real headache. Please help...
[eluser]pistolPete[/eluser]
Why do you need query strings at all? Did you have a look at this post? http://ellislab.com/forums/viewreply/389313/
[eluser]damienwjking[/eluser]
I did try the PATH_INFO and it didn't work, also tryed the MY_URI but because I need a url as one of the querys the / breaks the other code. Well thats what I think is happening. I have to use it this way as its posting from 3rd party. www.test.com/controller/function/?title=Damiens %Test&url=http://www.test2.com/help/codeigniter.html Any other ideas?
[eluser]TheFuzzy0ne[/eluser]
You need to encode the URI, as the forward slashes are seen as the start of another segment. Check out my library which takes care of this for you - http://ellislab.com/forums/viewthread/106502/
[eluser]damienwjking[/eluser]
Thanks Lollerskates, but I am very new at this and having some issues integrating. Haved added your MY_URI to library, how do I call it in and use it. My controller has a method called add i.e. below, what method in MY_URI do I call to get the get field back when the URL is www.test.com/controller/add/?title=Damien &url=http://www.test.com/test2/codeigniter/index.php function add($title = NULL, $url = NULL) { $this->load->library('MY_URI'); $this->MY_URI->assoc_to_alt_uri(); // output $title and $url echo $title . $url; } This errors with fatal undefinded property
[eluser]TheFuzzy0ne[/eluser]
It extends the core URI class, and loads automatically. You call it like this: Code: $arr = array( This will give you a string that looks like this: controller/add/titleamien/url:http://www.test.com/test2/codeigniter/index.php You can then use that string in your URL. To get the segments back from the next controller method, you can do: Code: $title = $this->uri->asegment('title'); Hope this helps. EDIT: The forum has decoded the encoded string, so it doesn't look like it actually will in your script.
[eluser]damienwjking[/eluser]
Ok so I have updated method to function add() { $arr = array( 'title' => 'Damien', 'url' => 'http://www.test.com/test2/codeigniter/index.php' ); $uri = $this->uri->assoc_to_alt_uri($arr, 'whaddado/add'); $title = $this->uri->asegment('title'); $url = $this->uri->asegment('url'); echo $title.$url; } Still returns nothing for this url. Why do I need to set the assoc_to_alt_uri as I will never know what the query for url and title will be, they are dynamic. Sorry to be confused, I don't understand why its so simple.
[eluser]TheFuzzy0ne[/eluser]
The first function encodes the URI so you can use it in a view or redirect. The second function is to be used in the receiving controller method (not the same method that you encoded it in), which decodes it. The class allows you to pass parameters via the URI.
[eluser]damienwjking[/eluser]
Still don't get anything back for $title or $url with the following url. Doesn't error just prints nothing. Sorry for bothering you but I really am struggling with this. http://www.test.com/controller/add?title.../index.php CONTROLLER... function add() { $title = $this->uri->asegment('title'); $url = $this->uri->asegment('url'); // load query //$title = $_GET['title']; //$url = $_GET['url']; echo 'title and url is '.$title.$url; }
[eluser]TheFuzzy0ne[/eluser]
I'm not sure if I'm explaining myself correctly, so here's a demonstration that will show how we pass arguments in the URI from func1 to func2. ./system/application/controllers/test.php Code: <?php And the view: ./system/application/views/test.php Code: <html> The index() method just displays the page, when it's submitted, it's sent to the func1() method, which processes it, and then redirects to func2 with a new URI. The func2() method converts the URI arguments back into their original form again, and displays them. Does that make sense now? That exact code doesn't work for me, as my server doesn't allow encoded slashes. I have created a workaround, but I've found a bug in it which I will fix tomorrow. Let me know if it doesn't work for you. |
Welcome Guest, Not a member yet? Register Sign In |