Constructing a variable name

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

ceetee
New php-forum User
New php-forum User
Posts: 8
Joined: Thu Jan 02, 2003 2:28 pm

Constructing a variable name

Postby ceetee » Fri Jan 31, 2003 11:26 am

I have a form processing script which I want to use for inserting values into the database. The form stores the items from a shopping cart. The names of the hidden form boxes go like this. code0, item0.. code1, item1.. etc.

So I had the idea for using isset on a constructed variable name. But all attempts have failed.

The second part of constructing the SQL string also doesn't work, it just inserts the value of $i instead! This was tested independently of the isset part.

Any ideas how I should be doing this?
<code>
for ($i=0;isset("$code".$i);$i++)
{
$query="insert into order_items (orderId, prodCode, item, quantity, price) values ('$orderId','$code".$i."','$description".$i."',$quantity".$i.",$price".$i.")";
mysql_query($query);
}

User avatar
Joan Garnet
Moderator
Moderator
Posts: 387
Joined: Sat Aug 03, 2002 2:56 am
Location: Mars
Contact:

Postby Joan Garnet » Sat Feb 01, 2003 3:21 pm

I'm not sure about this...

Code: Select all

<?php
for ($i=0;$i<sizeof($HTTP_POST_VARS)/5;$i++)
{
   $query="insert into order_items (orderId, prodCode, item, quantity, price) values (".$HTTP_POST_VARS["orderId"].",".$HTTP_POST_VARS["code"].",".$HTTP_POST_VARS["description"].",".$HTTP_POST_VARS["quantity"].",".$HTTP_POST_VARS["price"].")";
   mysql_query($query);
}
?>

:)

ceetee
New php-forum User
New php-forum User
Posts: 8
Joined: Thu Jan 02, 2003 2:28 pm

Postby ceetee » Sat Feb 01, 2003 4:58 pm

Did what I should have done to begin with :) and looked in the manual. I wasn't using eval correctly. The code I made below does the business, that is get the value of $code0, $code1 etc.

for ($i=0;;$i++)
{
$codei='$code'.$i;
eval("\$codei=$codei;");
if(!isset($codei)) break;
$descriptioni='$description'.$i;
eval("\$descriptioni=$descriptioni;");
$quantityi='$quantity'.$i;
eval("\$quantityi=$quantityi;");
$quantityi=(integer) $quantityi;
$pricei='$price'.$i;
eval("\$pricei=$pricei;");
$pricei=(double)$pricei;
$query="insert into order_items (orderId, prodCode, item, quantity,
price) values ('$orderId','$codei','$descriptioni',$quantityi,$pricei)";
mysql_query($query);
}


Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 2 guests