Moving away from $_GET (and querystrings) |
[eluser]FragPacket[/eluser]
Greetings, I'm working on converting an existing app to CI -- I don't have any great love of $_GET variables, but I'm having a hard time moving my app logic to use segmented URLs instead. Specifically I'm not sure how I should be handling multiple variables in one pass -- as an example, a URL I would have previously used was something like: Code: http://www.mysite.com/mylinks.php?search_term=flowers&sort_by=date I get segmented URLs conceptually, just not sure how they address the variable/value relationship when there's more than one pair. Thanks in advance, Michael
[eluser]xzela[/eluser]
Hi, You may want to look at the Controller User Docs a bit more. The params in the URL relate to params in the controller (see code below) For example, if you have a URL string like this: Code: www.mysite.com/mylinks.php?search_term=flowers&sort_by=02-02-2002 Code: www.mysite.com/my_links/flowers/02-02-2002 Code: function my_links($search_term, $date) { //You may want to default these params to null to avoid errors The controller will run some logic and then show the view. I hope this makes sense.
[eluser]Colin Williams[/eluser]
You can also do key/value pairs with parameters, like: http://www.example.com/mylinks/search/te...02-02-2002 And then use $this->uri->uri_to_assoc() to get these into an associative array.
[eluser]FragPacket[/eluser]
Thanks for the replies guys -- the $this->uri->uri_to_assoc() helper function was what I was after, just as a stop-gap for now. Eventually, I'll pull all this out and hide it from users with some AJAX calls, but for now this will get me by!
[eluser]gh0st[/eluser]
I understand that you can use $this->uri->uri_to_assoc() to get a URL, but what happens: 1) When the user types in "A really long phrase" -- you get: http://www.example.com/search/term/a really long phrase 2) How do you put the search phrase into the uri_to_assoc in the first place? I tried $this->uri->assoc_to_uri() but if the user enters "A really long phrase" you get the result shown in part 1.
[eluser]TheFuzzy0ne[/eluser]
[quote author="gh0st" date="1234196729"]I understand that you can use $this->uri->uri_to_assoc() to get a URL, but what happens: 1) When the user types in "A really long phrase" -- you get: http://www.example.com/search/term/a really long phrase [/quote] No, a browser will automatically encode the url. Try typing it into your own address bar and hitting enter. I get http://www.example.com/search/term/a% 20really% 20long% 20phrase (without the spaces). In some cases you won't see the encoding, but it is encoded nonetheless. [quote author="gh0st" date="1234196729"] 2) How do you put the search phrase into the uri_to_assoc in the first place? [/quote] Usually using a redirect. One page gets the users input, then that page will reformat the input (if needed), and redirect the user to the search page which actually performs the search and displays the results. [quote author="gh0st" date="1234196729"] I tried $this->uri->assoc_to_uri() but if the user enters "A really long phrase" you get the result shown in part 1.[/quote] |
Welcome Guest, Not a member yet? Register Sign In |