Generate $_POST Values

Hi

I would like to generate list of $_POST variables using records from database:
example:


$amount1=$_POST['amount1'];
$amount2=$_POST['amount2'];
$amount4=$_POST['amount4'];
$amount7=$_POST['amount7'];
... and so on ....

where number next to amount word is simply id value in the database. $_POST values i get from the form erlier.

I tried following way and some other ways too but doesnt seems to work (and im not even sure if possible).


$result2 = mysql_query("SELECT * FROM list") or die(mysql_error());
  		while($row2=mysql_fetch_array($result2))
		{

		$amo = "amount";	
		$amount = $row2['id'];
		$amo1 = $amo.$amount;
		$amo1 = $_POST['$amo1'];
		echo "($amo1)";

	        }

Hope is clear what i want to do!
Thank you very much in advance.

sounds like you need variable variables.

Directly do:


//-------------
$amo1 = $_POST['amount' . $row2['id']];
echo $amo1;
//-------------

Your problem here is about using quotes; single or double!

Great, thank you, its a progress…


$amo1 = $_POST['amount' . $row2['id']];

$_POST is genereted and is getting and displaying values from the form correctly.

But there is one more think:

From what i can understand whats happening now is


$amo1 = $_POST['amount' . $row2['id']]; //-- 1 - id value so $amo1 = amount1
$amo1 = $_POST['amount' . $row2['id']]; //-- 2 -than  $amo1 = amount2
$amo1 = $_POST['amount' . $row2['id']]; //-- 5 -than  $amo1 = amount5

Let say there is 50 id’s, what i want to achive is to generate variables according to id value:
Example:


$amo1 = $_POST['amount' . $row2['id']]; //-- 1 - id value so $amo1 = amount1
$amo2 = $_POST['amount' . $row2['id']]; //-- 2- than  $amo2 = amount2
$amo5 = $_POST['amount' . $row2['id']]; //-- 5 - than  $amo5 = amount5

How to generete variables $amo1, $amo2, $amo5 and … more … can that be done?

Thank you

The link I posted shows examples of what you want.

Or another option is to use an array instead of $amo1, $amo2, $amo5…

Use array instead of generating such more variables:


$result2 = mysql_query("SELECT * FROM list") or die(mysql_error()); 
$data = array();
while($row2 = mysql_fetch_array($result2)) { 
	$data['amount' . $row2['id']] = $_POST['amount' . $row2['id']];
}
print_r($data);

Another vote for an array, the less global variables the better. :slight_smile:

Hi

Thanks a lot for your help, Got it right ! Used Arrays.
Special thanks to Raju!