LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   PHP Question about POST data (https://www.linuxquestions.org/questions/programming-9/php-question-about-post-data-386584/)

BrianK 11-25-2005 10:27 PM

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";
while(true)
{
  $key = "item_name" . $i;
  $item_name[$i] = $_POST['$key'];
  if ($item_name[$i] = " ")
    break;
  ++$i;
}

..but that doesn't work. Evidently, I can't use "$key" like that because it doesn't work.

Is there some way to do this in a loop? Can you use a variable as a POST array index?

rylan76 11-25-2005 11:41 PM

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.

LiquidFire 11-26-2005 02:14 AM

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++)
{
  $key = "item_name" . $i;
  $item_name[$i] = $_POST[$key];
  if ($item_name[$i] = " ") break;
}


spooon 11-26-2005 04:20 AM

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++)
{
  $temp = $_POST["item_name$i"];
  if (isset($temp))
    $item_name[$i] = $temp;
  else
    break;
}


graemef 11-26-2005 08:40 AM

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"]))
{

  // do something with $_POST["item_names$i"]
  $i++;

}

graeme.

keefaz 11-26-2005 09:08 AM

I would do :
PHP Code:

$item_name = Array();
foreach(
$_POST as $key => $val) {
    if(
strpos($key'item_name') !== FALSE) {
        
$item_name[] = trim($val);
    }



rylan76 11-26-2005 11:25 AM

Re: Re: PHP Question about POST data
 
Quote:

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 :)
Code:

for($i="1"; true; $i++)
{
  $key = "item_name" . $i;
  $item_name[$i] = $_POST[$key];
  if ($item_name[$i] = " ") break;
}


Sure, the loop type doesn't matter - I think the quotes don't matter... not sure though. Oh, and rather make

if ($item_name[$i] = " ") break;

like this (my opinion only!)

if (strcasecmp($item_name[$i]," ") == 0) break;

Regards,

BrianK 11-26-2005 01:33 PM

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

Thanks again!


All times are GMT -5. The time now is 07:04 AM.