Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 23rd, 2003, 11:31 AM
Authorized User
 
Join Date: Oct 2003
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default ampersand in a querystring

Hi All
$cat = "Book";
$subCat = "Fiction & nonFiction";

For example:
<a href="xxx.php?category=<%print $cat;%>&subCat=<%print $subCat;%>"><% print $cat.$subCat; %>

What i want to do is to search in my database to find a match, then return details information for this record. The trouble is that it won't return anything if value of a variable includes "&". Is there a reason php rejects it? I tried to replace the & with and, then it worked, but that was not what i want. My background is asp, so i don't know why php has problem with the ampersand.

Please help. Thank you




 
Old October 23rd, 2003, 12:08 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

I assume you want to use the ampersand literally in the query string and not as an argument separator. In which case you could use the functions urlencode /urldecode. Which will encode the query string so you could avoid PHP treating an ampersand value as an argument separator.

http://www.php.net/urlencode
http://www.php.net/urldecode

So essentially you pass the $variable to your link like this:

<a href="xxx.php?category=<%print urlencode($cat);%>&subCat=<%print urlencode($subCat);%>"><% print urlencode($cat.$subCat); %>

And then pass the string back through urldecode.

ASP-style delimiters are a bad idea..
Here is an FAQ that I wrote that discusses why as well as how to use a standards-compliant url-embedded argument separator:
http://p2p.wrox.com/topic.asp?TOPIC_ID=4028

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old October 23rd, 2003, 03:22 PM
Authorized User
 
Join Date: Oct 2003
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you so much Smiling Souls,

I got it. Perfect.

trang

 
Old October 23rd, 2003, 07:54 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

<rant>
Please, for the love of all things decent, do NOT use ASP-style <% %> tags as PHP delimiters!!! You're much better off ALWAYS using <?php and ?>.
</rant>

=)

Take care,

Nik
http://www.bigaction.org/





Similar Threads
Thread Thread Starter Forum Replies Last Post
Ampersand SQL equivalent Coby Access VBA 1 May 4th, 2007 06:25 AM
ampersand in hyperlink is not working bcogney XML 5 August 23rd, 2006 04:27 PM
Escaping Ampersand Entities android66 Visual Basic 2005 Basics 2 February 5th, 2005 04:46 AM
AMPERSAND disappears when given along with # happygv Forum and Wrox.com Feedback 2 October 25th, 2004 09:49 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.