Page 1 of 1

Posting a variable using $_GET and the $ character breaks it

Posted: Sun Feb 27, 2005 6:29 am
by robster
Hi all,

I am posting variable using similar to this:
<a href = \"index.php?category=Dogs & Cats\">

When the url comes thru, it looks similar to this:
index.php?category=Dogs%20&%20Cats and my code of $cat = $_GET['category'] returns $cat as 'Dogs' and misses out on the rest!

What normally happens, is my site then goes thru the database, and retrieves any entries with Dogs & Cats in it, except, the & is breaking the search.

If I were to do a search on a category without an &, IE:
<a href = \"index.php?category=Dog Biscuits\">
then it comes out index.php?category=Dog%20Biscuits and it works a treat, but I HAVE to have the & in the url.

How can I overcome this? All my database entries (nearly 4500 of them) have an & in them!

Thanks so much, I really appreciate your time and help :)

Posted: Sun Feb 27, 2005 6:51 am
by robster
Was just thinking about this, the & character is used by PHP to define more variables...

So a URL like my one above would be sticking data after & into a variable (or at least trying, except it's a %20).

Is there any way to get around this? Seems a toughy, I could parse my database, string search for any & characters and replace them with a 'and' string?

Anyone think this is the best bet, or is there a way to use the data I am still using....

WHAT about (thinking out loud here).... keep the data in the database that I have (with the &'s in it) and when I retrieve it initially to render my menu, that is the time I change the '&' to an 'and', then, when it comes back as my $GET call, change it back to an '&' for use in the database.

I may try that, I'd appreciate any advice here though, maybe something more elegant, or is that perhaps as good as it gets? ;)

Thanks for reading, and I really love any advice given,

Posted: Sun Feb 27, 2005 7:57 am
by feyd
rawurlencode()