Welcome Guest, Not a member yet? Register   Sign In
Enabeling query strings in URL
#1

[eluser]Christophe28[/eluser]
Hi,

I'm creating a search functionality for my website.

When I search something and press the submit button the URL changes to example.com/search/ instead of example.com/search?q=this_is_what_i_am_searching

I want to enable this so search queries can be saved and send to friends and family. When I enable the configs query string, the q isn't attached to the URL.

Does anybody know how to fix this, without facing strange bugs in the application?

Best,
Christophe
#2

[eluser]Christophe28[/eluser]
Nobody?
#3

[eluser]Zaher Ghaibeh[/eluser]
to solve your problem , you can search the forum and you will find many solutions :
1- to store your query string in the database.
2- to store your query string in the session/cookies.

but i dont think you will find a solution to add the query string to your URI .
#4

[eluser]Christophe28[/eluser]
But the comments clearly says:

| The other items let you set the query string "words" that will
| invoke your controllers and its functions:
| example.com/index.php?c=controller&m=function

Or isn't this the same as what I want to achieve?
#5

[eluser]Nicholas Bello[/eluser]
Hey Christophe28,

It's possible to do both - to have some pages operate with query strings and others operate with the standard URL routing in the same application. First you need to turn query strings on in the config file. I actually did something similar to this about a year and a half ago. My solution was to have a search string like this:

/index.php?c=search&m=results&p=0&q=test+foo+bar

In the above example:

c = controller (called search)
m = the function in the controller (called results)
p/q = two parameters passed into the results function. p stands for the current page in a paginated list while q stands for the query.

The form looked like this

Code:
<form action="http://examplesite.com/index.php" id="searchForm" method="GET">            

<input type="hidden" name="c" value="search" />
<input type="hidden" name="m" value="results" />
<input type="hidden" name="p" value="0" />
            
<input type="text" name="q" id="searchBox" value="" />
<input type="submit" value="submit" name="submit" />

</form>

As a final trick you can remove the /index.php from the forms action if you have mod_rewrite enabled. This allows the query URL to be saved and sent with an even shorter string. Removing the index.php would result in a url like this

http://examplesite.com/?c=search&m=resul...st+foo+bar.

EDIT

To make the paragraph above more technically correct even if you don't have mod_rewrite enabled as long as the default index for your site (as set in your webserver) is index.php the above string will work as well (I'm like 98.9% sure about this - Test it if you want to try it out).

Let me know if that helps!

Nick
#6

[eluser]mah0001[/eluser]
Hi Christophe28, If you are using the latest CodeIgniter 2.0.2, there is builtin support to use segments and querystrings. All you need is to enable the setting enable_query_strings in your config.php.

M.
#7

[eluser]Christophe28[/eluser]
Using segments would be even better I think. I think I have version 2.0. Time to upgrade :-)

Regards!
#8

[eluser]Christophe28[/eluser]
PS: Thanks for your input Nicholas Bello. I have learned a lot :-)
#9

[eluser]Nicholas Bello[/eluser]
Hey Christophe28,

No problem, happy to be of assistance. ;-)

Nick




Theme © iAndrew 2016 - Forum software by © MyBB