This is a discussion on Matching GET values... within the PHP Language forums, part of the PHP Programming Forums category; All, I am trying to pass each GET variable to a function and check if it was expected. For example, ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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=filename&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","file","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=filename&func=1&bad=no tsupposedtobehere then I would see 3 "matches" and 1 "no match" for the "bad" parameter. I hope this makes some sense =) Thanks. |
|
|||
|
Dimension7 wrote:
> 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=filename&func=1 (...) > 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; What's this $bmchk for ? You do not use at all! Well ... I do :) > while (list ($key, $val) = each ($bmc)) { > if ($val == $g) { $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>"; #> } > } 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>"; > } > > $bmc = array("name","file","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=filename&func=1&bad=no tsupposedtobehere > then I would see 3 "matches" and 1 "no match" for the "bad" parameter. > > I hope this makes some sense =) 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 : |
|
|||
|
Dimension7 <dimension@seven.com> wrote:
> $bmc = array("name","file","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=filename&func=1&bad=no tsupposedtobehere > then I would see 3 "matches" and 1 "no match" for the "bad" parameter. With bmcheck you are trying to reinvent array_key_exists(), sounds kind of silly to me :) function bmcheck($arr,$key) { if(array_key_exists($key,$arr)) { echo "match"; } else { echo "nomatch"; } } -- Daniel Tryba |
|
|||
|
On 29 Apr 2004 08:37:27 GMT
Pedro Graca <hexkid@hotpop.com> wrote: > Dimension7 wrote: > > 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=filename&func=1 > (...) > > 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; > > What's this $bmchk for ? You do not use at all! > Well ... I do :) > > > while (list ($key, $val) = each ($bmc)) { > > if ($val == $g) { > $bmchk = 1; break; > } You found what you're looking for, just stop then. |