PDA

View Full Version : cycle through various POST array variables and types


AmericanWoman
08-09-2003, 04:17 AM
I know I remember this from way back when, but I need to find some code that cycles through all the variables of a POST array...and if the variable is, itself, an array, it splits it into its individual values.

For instance, my config screen has a multiple select box, in addition to several regular text boxes, etc. While the text boxes will return a single value, the select box returns an array. I'm hoping to find some simple code to cycle through both types, and output the name/value pairs to a querystring.

Any ideas? :)

g-force2k2
08-09-2003, 10:39 PM
if you're reffering to the HTTP_POST_VARS then you can cycle through them:

while( list( $key, $val ) = each( $HTTP_POST_VARS ) )
{
echo "<b>" . $key . ":</b>" . $val . "<br>";
}

it will print out each of the post names with their values...

and you can adjust the script to get the effect you're looking for...

regards,
g-force2k2

AmericanWoman
08-09-2003, 10:44 PM
You rock, thank you. I can then extrapolate the same function for finding out whether the key holds an array, I would think, like so:


if (is_array($HTTP_POST_VARS['$key'])) {
do the same while function
}


I'll let you guys know how it comes out. The result of all this stuff may be my first hack. :P Here's hoping!

Thanks. I'm an ass. Should have known. :)

g-force2k2
08-09-2003, 10:51 PM
well the actual array values are seperated into their respected values...

if you're trying to get the array of values and search though them then i think this is what you should use...

for instance for an input value to be an array it would require the name to be

<input type="text" name="varname[]" value="">

then after you submit the data you just do:

while( list( $key, $val ) = each( $varname ) )
{
echo "<b>" . $key . ":</b> " . $val . "<br>";
}

i think this is the best way to get arrays from your data, pretty much your data will only be an array if you dictate it to be in the input values...

regards,
g-force2k2

AmericanWoman
08-09-2003, 11:56 PM
I'm not searching through the array; I'm outputting all the values passed.

The script to parse ALL the post vars worked fantastic, but I ran into a problem trying to handle post arrays. Here's the code:


if (isset($submit)) {
$scriptmid = "?foo=bar";
while (list($key,$value) = each($POST_VARS)) {
//echo "Starting POST var loop.";
if ($key <> "submit" && $key <> "thecode") {
if (is_array($key)) {
//echo "POST var is array - starting loop.";
$scriptmid = $scriptmid . "&" . $key . "=[";
while (list($pkey,$pvalue) = each($key)) {
$scriptmid = $scriptmid . $pvalue . ",";
}
$scriptmid = $scriptmid . "]";
}
else {
$scriptmid = $scriptmid . "&" . $key . "=" . $value;
}
}
}
}


It recognizes that the one field I'm looking for is an array, but it doesn't output the right information from the array handling loop.

For what I'm trying to do, take a look at the following:

www.acuraworld.com/forums/jsconfig.php

The script is being used to output a customized syndicated content feed. All the other vars are working fine, but I'm not having any luck passing forumid (look at the script tag in the textarea at the bottom of the screen after you've selected a couple of forums).

g-force2k2
08-10-2003, 12:03 AM
ah didn't know you were using a multiple select form :p

so thats the array that you're trying to get the data for yes? ( which represent the forumids )

regards,
g-force2k2

AmericanWoman
08-10-2003, 12:05 AM
Yeah - as you can see from the script tag it outputs, it just says "forumid=Array"...even though, when I uncomment my loop statements for debugging, it IS entering the is_array loop structure. :(

I don't get it! I'm very close to having this thing done, and I really wish it would work, already! :P

g-force2k2
08-10-2003, 12:12 AM
yes well looking at the code no matter how many ones you select the form will only return one id on a multiple select that i was just testing... any ideas on how to get all of the values that are selected?

g-force2k2

AmericanWoman
08-10-2003, 12:14 AM
? The code output at the top that says "Option ## selected" shows you the multiple options passed...? This same type of loop is generating that output, except the post variable name I'm looking for is specified instead of inferred.

Works fine on my end, as far as passing multiple values goes. It's just getting them to display that's the problem. :(

g-force2k2
08-10-2003, 12:19 AM
okay my bad i got the array to pass as well, but what do you mean you can get them to display?

when i submit it says what options are selected but what effect were you trying to achieve?

err... nvm i see the string that you're trying to make but is that the problem that its not coming out as you wanted it to?

g-force2k2

AmericanWoman
08-10-2003, 12:27 AM
The form generates a script tag, e.g.:


<script language="javascript" src="blah.php?forumid=[x,x,x,x]&font=Verdana&border=0"></script>


The script file needs to be able to parse through the forumid array and build the "where" clause of the sql statement accordingly.

I've got all the colors, etc., working, but the forumid part is the most important...and right now, it's just saying "forumid=Array".

I'm trying to figure out why. :(

g-force2k2
08-10-2003, 12:32 AM
i think thats because both php and javascript take "[" "]" and treat it like its an array... try and remove them from the code

g-force2k2

AmericanWoman
08-10-2003, 12:36 AM
It wasn't in the code, originally, and it still output the same thing, but hang on, I'll try it again for kicks.

Dangit. I really wish there were a way to just beam code down to Earth. :(

g-force2k2
08-10-2003, 12:51 AM
this is the code i get in my test....

test.php?&forumid=[10,11,12,13,14,15,16,17,18,]

we have to be doing somethign different...

[edit] nice job :)

g-force2k2

AmericanWoman
08-10-2003, 12:52 AM
Figured it out!

I don't know WHY, but for whatever reason, in the second loop (testing whether the POST var is an array, then writing the output), I have to refer to the POST var as $POST_VARS[$key] instead of just $key.

Woot!

g-force2k2
08-10-2003, 12:57 AM
im glad that you figured it out

good luck with the script :)

g-force2k2

AmericanWoman
08-10-2003, 01:14 AM
Thanks! If I get it working really well, I'll put it in the beta release forum on Monday. :)

I appreciate all your help.

Dean C
08-10-2003, 10:19 AM
Interesting... You use PHP3's method of looping through an array :)

AmericanWoman
08-10-2003, 12:04 PM
Today at 07:19 AM Mist said this in Post #18 (https://vborg.vbsupport.ru/showthread.php?postid=424809#post424809)
Interesting... You use PHP3's method of looping through an array :)

I used the loop structure I was given. :p It's been a very long while since I was a web developer for a living, and I'm terribly rusty but determined to muddle through!

What syntax would you have used?