PHP Question about POST data
I'm getting a bunch of POST data from paypal. In that data, there exists a variable "item_nameX" where X is a number based on the number of different items the person ordered... so I get item_name1, item_name2, item_name3 and so on.
I can access the data with $_POST['item_name1']; but because I don't know how many there are, I've put the assignment in a little loop: Code:
$i="1"; Is there some way to do this in a loop? Can you use a variable as a POST array index? |
Hi Brian,
I've done exactly this - your logic is correct, something else must be wrong. I. e. I have a form being posted that contains 10 fields, and I enumerate all 10 fields when processing the form using $fieldname = "form_field_"; $counter = 0 $num_fields = 10; while ($num_fields > 0) { $result = $HTTP_POST_VARS[$fieldname + $counter]; //work with result $counter++; //Whoops! $num_fields--; } This enumerates all 10 fields correctly... I suspect something else must be awry though I can't spot it, since your code seems 100% correct. |
Re: PHP Question about POST data
First of all, I'm not sure if this is the cause of the problem, but wouldn't the index of the array be without quotes, or is $_POST an exception or something? Also, wouldn't it be better using a for loop rather than a while loop, much cleaner :)
Code:
for($i="1"; true; $i++) |
Re: Re: PHP Question about POST data
I'm not sure if your condition for ending the loop works. I would do something like this:
Code:
for ($i = 1; true; $i++) |
loop on POST variable
Okay you want your loop to see if the variable has been set up. This can be done as follows:
Code:
while (is_set($_POST["item_names$i"])) |
I would do :
PHP Code:
|
Re: Re: PHP Question about POST data
Quote:
if ($item_name[$i] = " ") break; like this (my opinion only!) if (strcasecmp($item_name[$i]," ") == 0) break; Regards, |
Thanks for the help guys - finally got it working.
My first major problem was '=' instead of '==' in the if test. Because it was an assignment, it was always true, so it always broke out of the loop. Secondly, the test was wrong, I should have been using empty(). (BTW: isset didn't work - at least I couldn't get it working... I don't know why it didn't work when checking the $_POST array, but I can see it wouldn't work after the assignment, because you just set it to nothing). The for loop does look much cleaner. working: Code:
for($i="1"; $i<100; $i++) Thanks again! |
All times are GMT -5. The time now is 07:04 AM. |