Matching GET values...

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

    Matching GET values...

    All,
    I am trying to pass each GET variable to a function and check if it was
    expected.
    For example, here is a sample link:

    index.php?name= modname&file=fi lename&func=1

    the problem is, instead of returning:
    match
    match
    match

    it returns:

    match
    no match
    no match
    no match
    match
    no match
    no match
    no match
    match

    I know the problem is something with the =each, because it is evaluating
    each item against each other, I just don't know how to fix it.

    function bmcheck ($bmc, $g)
    {
    $bmchk = 0;
    while (list ($key, $val) = each ($bmc)) {
    if ($val == $g) {
    echo "match";
    echo "<br>";
    } else {
    echo "no match";
    echo "<br>";
    }
    }
    }

    $bmc = array("name","f ile","func");
    foreach ($_GET as $gvalue => $g) {
    bmcheck($bmc, $gvalue);
    }

    Basically, when I'm done, if something like this was passed:
    index.php?name= modname&file=fi lename&func=1&b ad=notsupposedt obehere
    then I would see 3 "matches" and 1 "no match" for the "bad" parameter.

    I hope this makes some sense =)
    Thanks.


  • Pedro Graca

    #2
    Re: Matching GET values...

    Dimension7 wrote:[color=blue]
    > All,
    > I am trying to pass each GET variable to a function and check if it was
    > expected.
    > For example, here is a sample link:
    >
    > index.php?name= modname&file=fi lename&func=1[/color]
    (...)[color=blue]
    > I know the problem is something with the =each, because it is evaluating
    > each item against each other, I just don't know how to fix it.
    >
    > function bmcheck ($bmc, $g)
    > {
    > $bmchk = 0;[/color]

    What's this $bmchk for ? You do not use at all!
    Well ... I do :)
    [color=blue]
    > while (list ($key, $val) = each ($bmc)) {
    > if ($val == $g) {[/color]
    $bmchk = 1;
    }

    $bmchk was initialized to 0 before the while() and will only get set to
    1 if at least one of the $bmc keys matches the $g parameter.

    #> echo "match";
    #> echo "<br>";
    #> } else {
    #> echo "no match";
    #> echo "<br>";
    #> }[color=blue]
    > }[/color]

    Now all keys have been checked and $bmchk is either 0 (no matches) or 1
    (1 or more matches)

    if ($bmchk) echo "match<br>" ;
    else echo "no match<br>";
    [color=blue]
    > }
    >
    > $bmc = array("name","f ile","func");
    > foreach ($_GET as $gvalue => $g) {
    > bmcheck($bmc, $gvalue);
    > }
    >
    > Basically, when I'm done, if something like this was passed:
    > index.php?name= modname&file=fi lename&func=1&b ad=notsupposedt obehere
    > then I would see 3 "matches" and 1 "no match" for the "bad" parameter.
    >
    > I hope this makes some sense =)[/color]

    I hope my post makes sense :-)

    --
    USENET would be a better place if everybody read: : mail address :
    http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
    http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
    http://www.expita.com/nomime.html : to 10K bytes :

    Comment

    • Daniel Tryba

      #3
      Re: Matching GET values...

      Dimension7 <dimension@seve n.com> wrote:[color=blue]
      > $bmc = array("name","f ile","func");
      > foreach ($_GET as $gvalue => $g) {
      > bmcheck($bmc, $gvalue);
      > }
      >
      > Basically, when I'm done, if something like this was passed:
      > index.php?name= modname&file=fi lename&func=1&b ad=notsupposedt obehere
      > then I would see 3 "matches" and 1 "no match" for the "bad" parameter.[/color]

      With bmcheck you are trying to reinvent array_key_exist s(), sounds kind
      of silly to me :)

      function bmcheck($arr,$k ey)
      {
      if(array_key_ex ists($key,$arr) )
      {
      echo "match";
      }
      else
      {
      echo "nomatch";
      }
      }


      --

      Daniel Tryba

      Comment

      • Kelly Thompson

        #4
        Re: Matching GET values...

        On 29 Apr 2004 08:37:27 GMT
        Pedro Graca <hexkid@hotpop. com> wrote:
        [color=blue]
        > Dimension7 wrote:[color=green]
        > > All,
        > > I am trying to pass each GET variable to a function and check if
        > > it was expected.
        > > For example, here is a sample link:
        > >
        > > index.php?name= modname&file=fi lename&func=1[/color]
        > (...)[color=green]
        > > I know the problem is something with the =each, because it is
        > > evaluating each item against each other, I just don't know how to
        > > fix it.
        > >
        > > function bmcheck ($bmc, $g)
        > > {
        > > $bmchk = 0;[/color]
        >
        > What's this $bmchk for ? You do not use at all!
        > Well ... I do :)
        >[color=green]
        > > while (list ($key, $val) = each ($bmc)) {
        > > if ($val == $g) {[/color]
        > $bmchk = 1;[/color]

        break;
        [color=blue]
        > }[/color]

        You found what you're looking for, just stop then.

        Comment

        Working...