Welcome Guest, Not a member yet? Register   Sign In
Simple Query String Handler
#1

[eluser]virexmachina[/eluser]
After searching through the forums and wiki for a simple way to handle query strings in CI, I ended up writing a simple helper. I only needed to receive a query string on one page, so I didn't want to have to enable hooks, change uri_protocol or anything. This is good for cases where you need to get query strings on a page or two, like a search page or confirmations from payment gateways.

Anyway, here's a simple way to access the query string from any controller:

Code:
// for example, we're calling foo.com/search/?term=foo&bar=goo&baz;
$this->load->helper('query');
$qs = new Query_String();
$term = $qs->variable("term"); // returns foo
$bar = $qs->variable("bar"); // returns goo
$baz = $qs->variable("baz"); // returns true
$gaz = $qs->variable("gaz"); // returns NULL
$qsarray = $qs->all(); // returns Array([term] => foo, [bar] => goo, [baz] => NULL)

I tried to follow CI Input class conventions of returning null for variables that do not exist. I also return true if a var is provided with no value.

Download the helper to application/helpers and you're ready to go.

I adapted a loop from Dan Horrigan's CodeIgniter Query String hook to look for the query string in a few places.

Corrections and suggestions are very welcome.


Messages In This Thread
Simple Query String Handler - by El Forum - 01-02-2011, 02:52 PM
Simple Query String Handler - by El Forum - 01-04-2011, 09:36 PM
Simple Query String Handler - by El Forum - 01-05-2011, 09:33 AM
Simple Query String Handler - by El Forum - 01-05-2011, 09:59 AM
Simple Query String Handler - by El Forum - 03-04-2011, 02:49 AM



Theme © iAndrew 2016 - Forum software by © MyBB