LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-25-2005, 10:27 PM   #1
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
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?
 
Old 11-25-2005, 11:41 PM   #2
rylan76
Senior Member
 
Registered: Apr 2004
Location: Potchefstroom, South Africa
Distribution: Fedora 17 - 3.3.4-5.fc17.x86_64
Posts: 1,508

Rep: Reputation: 100Reputation: 100
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.

Last edited by rylan76; 11-26-2005 at 11:23 AM.
 
Old 11-26-2005, 02:14 AM   #3
LiquidFire
LQ Newbie
 
Registered: Nov 2005
Location: Sofia, Bulgaria
Distribution: Gentoo
Posts: 5

Rep: Reputation: 0
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;
}
 
Old 11-26-2005, 04:20 AM   #4
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
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;
}
 
Old 11-26-2005, 08:40 AM   #5
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
Old 11-26-2005, 09:08 AM   #6
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
I would do :
PHP Code:
$item_name = Array();
foreach(
$_POST as $key => $val) {
    if(
strpos($key'item_name') !== FALSE) {
        
$item_name[] = trim($val);
    }

 
Old 11-26-2005, 11:25 AM   #7
rylan76
Senior Member
 
Registered: Apr 2004
Location: Potchefstroom, South Africa
Distribution: Fedora 17 - 3.3.4-5.fc17.x86_64
Posts: 1,508

Rep: Reputation: 100Reputation: 100
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,
 
Old 11-26-2005, 01:33 PM   #8
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51
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!

Last edited by BrianK; 11-26-2005 at 01:36 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Post data expired from cache Madrinator Programming 1 12-16-2004 02:17 PM
php, apache, POST/GET data jhaiduce Linux - Software 0 12-17-2003 04:02 PM
Trouble recognising 'post' data in a php file davee Programming 4 10-20-2003 12:29 PM
php post data sql queries... bulliver Programming 2 01-03-2003 02:18 AM
post data in linux qiaob Programming 2 01-15-2002 07:18 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration