ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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:
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.
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
Originally posted by LiquidFire 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
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++)
{
$key = "item_name" . $i;
$item_name[$i] = $_POST[$key];
// lots of other variable assignments here
if (empty($item_name[$i]))
break;
}
the $i<100 keeps out of infinite loops if something goes wrong
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.