Get vars in URL

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marc Labtec

    Get vars in URL

    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
  • Nel

    #2
    Re: Get vars in URL

    "Marc Labtec" <php@komatia.co m> wrote in message
    news:ada5a15f.0 406110106.61dca c51@posting.goo gle.com...[color=blue]
    > 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[/color]
    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


    Comment

    • David Mackenzie

      #3
      Re: Get vars in URL

      On Fri, 11 Jun 2004 10:53:37 +0100, "Nel" <nelly@ne14.co. NOSPAMuk>
      wrote:
      [color=blue]
      >"Marc Labtec" <php@komatia.co m> wrote in message
      >news:ada5a15f. 0406110106.61dc ac51@posting.go ogle.com...[color=green]
      >> 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[/color]
      >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 }[/color]

      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:[color=blue][color=green]
      >> 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...[/color][/color]

      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 )

      Comment

      • Brad Kent

        #4
        Re: Get vars in URL

        $result = ( count(array_key s($_GET,1)) == 1 ); // $result is true or false

        php@komatia.com (Marc Labtec) wrote in message news:<ada5a15f. 0406110106.61dc ac51@posting.go ogle.com>...[color=blue]
        > 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[/color]

        Comment

        • Andy Hassall

          #5
          Re: Get vars in URL

          On 11 Jun 2004 02:06:12 -0700, php@komatia.com (Marc Labtec) wrote:
          [color=blue]
          >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...[/color]

          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 <andy@andyh.co. uk> / Space: disk usage analysis tool
          http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space

          Comment

          Working...