Multiple $_GET parameters

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
giles
Forum Commoner
Posts: 34
Joined: Thu Sep 14, 2006 2:34 pm

Multiple $_GET parameters

Post by giles »

Hi all,

I'm working on a development where I need to present the user with the option to change a number of forms before committing. Spaced out for reading, the passed url looks a little like this :

/index.php?
action=xyz&job_id=20&print=1&
action=xyz&job_id=25&print=1&
action=xyz&job_id=26&print=1&
action=xyz&job_id=27&print=1&
action=xyz&job_id=39&print=1

I know that $_GET and $_POST are forms of arrays. I’ve tried looking at them with the likes of print_r($_GET[’job_id’]), with little success.

So my basic question is : how do I handle multiple get or post parameters. I’d appreciate your comments

thank you
giles
soulcrazy
Forum Newbie
Posts: 13
Joined: Sun Aug 24, 2008 10:05 pm
Location: Ontario, Canada

Re: Multiple $_GET parameters

Post by soulcrazy »

First:

Code: Select all

 
echo print_r($_GET);  // don't specify the key if you want to display all pairs in the array
 

Second, there are many approaches you can take. I like to:

Code: Select all

 
$getArray = explode('&', $_SERVER['QUERY_STRING']);
 
print_r($getArray) would look like:

$getArray(
[0] => 'action=xyz',
[1] => 'job_id=20',
[2] => 'print=1',
[3] => ''
);

You could the use explode again within each:

Code: Select all

$action = explode('=', $getArray[0]);
Although, the result is much the same as print_r($_GET);
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: Multiple $_GET parameters

Post by Ziq »

Replace this
giles wrote: /index.php?
action=xyz&job_id=20&print=1&
action=xyz&job_id=25&print=1&
action=xyz&job_id=26&print=1&
action=xyz&job_id=27&print=1&
action=xyz&job_id=39&print=1
on

Code: Select all

 
/index.php?
action[]=xyz&job_id[]=20&print[]=1&
action[]=xyz&job_id[]=25&print[]=1&
action[]=xyz&job_id[]=26&print[]=1&
action[]=xyz&job_id[]=27&print[]=1&
action[]=xyz&job_id[]=39&print[]=1
 
For example: (index.php)

Code: Select all

 
print_r($_GET);
 
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Multiple $_GET parameters

Post by pickle »

You need to re-examine your approach. Why not something like:

index.php?xyz=1&print=1&job_id=20,25,26,27,39

Then use explode() to get all the job ids.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Multiple $_GET parameters

Post by RobertGonzalez »

Are you talking multiple GET variables with the same name or just more than one querystring param?
giles
Forum Commoner
Posts: 34
Joined: Thu Sep 14, 2006 2:34 pm

Re: Multiple $_GET parameters

Post by giles »

Thank you all for your comments

I can see that I might need to think on this a little. Here’s what I’m trying to do in a little more detail. I’m developing an online label application. I have a visitor making, recalling, and editing labels. The number of labels is totally flexible .... the serial number of each being cached in an array. just before printing, the array reads out the labels into a page where the visitor can select the number of each to print.

$action is the receiving script argument, xyz being the relevant case argument, so these need to be the same (I understand that this is also where I get into the problem of “over writing” values), job_id is the serial number of each record, print is the number of each label to print.

I can see that I need to get to something like......

index.php?action=xyz&job_id=20,25,26,27&print=1,1,2,1

but I’m not sure how to get there ... any thoughts?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Multiple $_GET parameters

Post by RobertGonzalez »

What is building that query string?
Post Reply