GET query

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aristotle
Forum Newbie
Posts: 2
Joined: Mon Jan 24, 2011 2:54 am

GET query

Post by aristotle »

Hi, I have written a code that take the GET contents of a URL , i.e after the ? and output it as a csv file.
eg - ....../xyz.php?str=<tr><td>c1</td><td>c2</td><td>c3..........c7</tr></td>

$input = $_GET['str'];
.........
............
......


output should be as - c1,c2,c3,c4.... so on
well , i have written a code for that and it works wonderfully except that if the url contains '&' and '#'(hidden fields) in the columns , the script malfunctions coz they act as delimiters.How do I take care of that? how to fetch the contents of the URL so that the & and # dont create a problem?

Please help.. :)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: GET query

Post by pickle »

If you're generating the URL, use urlencode(), then urldecode() after you've converted to CSV.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
aristotle
Forum Newbie
Posts: 2
Joined: Mon Jan 24, 2011 2:54 am

Re: GET query

Post by aristotle »

NO, Im not generating the links .I am jusy typing it in the browser address bar...
it will be encoded in some button later on.. but what i want is this only... the columns I pass... in str ,
eg.- ...?str='<tr><td>c1</td><td>c2</td><td>c3</td></tr>'
are to be made into csv format..which i ve done but my problem is that if the columns i pass contain # or & , the csv line breaks before that character.
How to 'treat' the input before using $_GET[] coz $_GET[] cannot take any string that has # or & in it.
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: GET query

Post by cpetercarter »

Your underlying problem is that http://www.mysite.com?str='<tr><td>c1</ ... 3</td></tr>' is not an allowed url. You cannot put characters like < > ' # or & into a url and expect it to work. You need to urlencode such characters first, before you put them in the url, as pickle has told you. (But you don't need to urldecode the query string when you retrieve it in the $_GET array - php does this job for you.)

More generally, I wonder why you want to transfer a set of values to csv format by trying to send an html table row in a url query string. It would be much easier to use ..?val1=c1&val2=c2&val3=c3 .... (Remember to urlencode c1, c2, c3 etc before you construct the query string). Then construct the csv string from the $_GET array, perhaps using implode()..
Post Reply