By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,830 Members | 2,276 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,830 IT Pros & Developers. It's quick & easy.

Get vars in URL

P: n/a
Hi all,

:)

I have an URL like this index.php?a=0&b=0&c=1&d=0&e=0&f=0

My prob now is: how can I find out if only *one* get var (a,b,d,d,e,f)
is 1?
I dopn't care about the other, i must detect, if *only one* var in the
url has its value 1, and all others are 0 then...

Thanks in advance - you are a great group here!

Marc
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Nel
"Marc Labtec" <ph*@komatia.com> wrote in message
news:ad**************************@posting.google.c om...
Hi all,

:)

I have an URL like this index.php?a=0&b=0&c=1&d=0&e=0&f=0

My prob now is: how can I find out if only *one* get var (a,b,d,d,e,f)
is 1?
I dopn't care about the other, i must detect, if *only one* var in the
url has its value 1, and all others are 0 then...

Thanks in advance - you are a great group here!

Marc

I am sure there are several ways to do this, but if you know that the values
will always be 1 or 0, you could simply add the values together.

if ($a+$b+$c+$d+$e+$f == 1) { OK } else { NOT OK }

Nel
Jul 17 '05 #2

P: n/a
On Fri, 11 Jun 2004 10:53:37 +0100, "Nel" <ne***@ne14.co.NOSPAMuk>
wrote:
"Marc Labtec" <ph*@komatia.com> wrote in message
news:ad**************************@posting.google. com...
Hi all,

:)

I have an URL like this index.php?a=0&b=0&c=1&d=0&e=0&f=0

My prob now is: how can I find out if only *one* get var (a,b,d,d,e,f)
is 1?
I dopn't care about the other, i must detect, if *only one* var in the
url has its value 1, and all others are 0 then...

Thanks in advance - you are a great group here!

Marc

I am sure there are several ways to do this, but if you know that the values
will always be 1 or 0, you could simply add the values together.

if ($a+$b+$c+$d+$e+$f == 1) { OK } else { NOT OK }


But as they're being passed by GET, none of the values can be
guaranteed.

Also, what about these scenarios:

index.php?a=-1&b=1&c=1&d=0&e=0&f=0

index.php?a=0&b=1&c=2&d=3&e=4&f=5

index.php?a=0&b=0&c=1&d=0&e=0&f=0&g=1&h=1&i=0

index.php?a=1

From the OP:
My prob now is: how can I find out if only *one* get var (a,b,d,d,e,f)
is 1?
I dopn't care about the other, i must detect, if *only one* var in the
url has its value 1, and all others are 0 then...


There is a contradiction here. He says "I don't care about the other",
but then says "if *only one* var in the url has its value 1, and all
others are 0 then..."

So, must the other GET variables be zero or not?

You could iterate through the $_GET[] array, making a count of the 1's
and 0's and then test those counts. The sum of the counts should equal
the number of GET variables.

--
David ( @priz.co.uk )
Jul 17 '05 #3

P: n/a
$result = ( count(array_keys($_GET,1)) == 1 ); // $result is true or false

ph*@komatia.com (Marc Labtec) wrote in message news:<ad**************************@posting.google. com>...
Hi all,

:)

I have an URL like this index.php?a=0&b=0&c=1&d=0&e=0&f=0

My prob now is: how can I find out if only *one* get var (a,b,d,d,e,f)
is 1?
I dopn't care about the other, i must detect, if *only one* var in the
url has its value 1, and all others are 0 then...

Thanks in advance - you are a great group here!

Marc

Jul 17 '05 #4

P: n/a
On 11 Jun 2004 02:06:12 -0700, ph*@komatia.com (Marc Labtec) wrote:
I have an URL like this index.php?a=0&b=0&c=1&d=0&e=0&f=0

My prob now is: how can I find out if only *one* get var (a,b,d,d,e,f)
is 1?
I dopn't care about the other, i must detect, if *only one* var in the
url has its value 1, and all others are 0 then...


Something like:

<?php
$matchVar = '';
$ok = false;
foreach (array('a','b','c','d','e','f') as $var)
if (@$_GET[$var] == 1)
if ($matchVar) // double match
$ok = false;
else { // found one
$matchVar = $var;
$ok = true;
}
elseif (@$_GET[$var] != 0) // bad value
$ok = false;

if ($ok)
print "Match found = $matchVar";
else
print "Some invalid combination";

?>

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.