Re: Using $HTTP_POST_VARS
| From: | Tiger Quimpo | Date: | Mon, 08 Jan 2001 03:42:37 +0000 |
| Subject: | Re: Using $HTTP_POST_VARS | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-33181@lists.php.net to get a copy of this message | ||
On Fri, 05 Jan 2001, Todd Cary wrote:
> $HTTP_POST_VARS contains variables like cans_1=a,cans_2=c,cans_3=b *and*
> sans_1=a,sand_2=d,sans_3=a.
>
> I want to take all of the cans_x variables and put them into an array
> keyed on the 1,2,3. For example, MyArray1 would contain from the above
> example $MyArray1[1]=>a,$MyArray1[2]=>c,$MyArray1[3]=>b.
>
> Then I want to do the same with the sans_x variables. This is so I can
> compare the values to see if they are the same. For example, does
> $MyArray1[1]==$MyArray2[1]?
>
> Can this be done? Can a wildcard be used to get all variables like
> "cans_..."?
this is probably a FAQ. but i'm not sure since i haven't read the FAQ
yet :).
the simple way would be to not use cans_* and sans_* variables in
there. instead, use an array, e.g.
<input name="cans[1]" value="xxx">
if you do that for all the cans_* variables, in PHP you'll have a $cans
array in HTTP_POST_VARS. just grab the array entries ($cans[1],
$cans[2], etc).
do the same for sans.
if you want to do it the hard way for some reason, it wouldn't be hard
to write a program that scans through $HTTP_POST_VARS for variables of a
certain pattern and write them into an array, e.g.
/* warning, code written in email client, untested
pattern would be something like "cans_" or "sans_"
or whatever.
*/
function myFunc($pattern)
{
global $HTTP_POST_VARS;
$keys=array_keys($HTTP_POST_VARS);
$ret=array();
$patlen=strlen($pattern);
// or use while(each(....)) or something.
for($ctr=0;$ctr<count($keys);$ctr++)
{
$k=$keys[$ctr];
/* warning, probably off by one errors there :). actually,
you should probably use regexps or sscanf there. or
write more code to make it more robust. this is just a
demo OTTOMH*/
if(substr($k,0,$patlen)==$pattern)
{
// yeah, and there too :).
$nx=(int)substr($k,$patlen+1);
$ret[$nx]=$HTTP_POST_VARS[$key];
}
}
return $ret;
}
in the caller, you could then just assign to whatever, e.g.,
$cans=myFunc("cans_");
$sans=myFunc("sans_");
this works in apache. no idea if it works in other web servers.
probably does though.
tiger
--
Gerald Timothy Quimpo tiger@kami.com http://members.xoom.com/TigerQuimpo
entia non sunt multiplicanda veritas liberabit vos
praetere necessitatem mene sakhet ur-seveh