Jump to content


Photo

Variable within Variable


  • Please log in to reply
4 replies to this topic

#1 adamjnz

adamjnz
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts

Posted 10 April 2006 - 06:44 AM

<?php echo $_POST[$row_products['productID']];?>

I am trying to get a variable within a variable. Basically the $_POST variable is a number that relates to productID in the products table. so for example if $row_products['productID'] was 1 then the echo would display the variable $_POST['1']

What is wrong with my code? It doesn't give me any errors it just doesnt come up with anything

#2 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 10 April 2006 - 06:46 AM

the problem is that you need to use braces, otherwise you are breaking the array syntax:

$_POST[{$row_products['productID']}]

give that a shot.

#3 adamjnz

adamjnz
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts

Posted 10 April 2006 - 06:49 AM

[!--quoteo(post=363201:date=Apr 10 2006, 07:46 PM:name=akitchin)--][div class=\'quotetop\']QUOTE(akitchin @ Apr 10 2006, 07:46 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
the problem is that you need to use braces, otherwise you are breaking the array syntax:

$_POST[{$row_products['productID']}]

give that a shot.
[/quote]

Got this error:

Parse error: parse error, unexpected '{', expecting ']' in .../testsite/profile/site/order.php on line 92

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 10 April 2006 - 02:38 PM

The original syntax is fine, the problem is that there is no index in the $_POST array that corresponds to the computed index.

Since what is contained in the $_POST array comes from names in a form, and those names can't be numeric, you can never have an index of "1" or "'1'". Please use the following code to dump the contents of the $_POST array when your script starts:
<?php echo '<pre>' . print_r($_POST,true) . '</pre>'; ?>
and see what is coming from the form and how it relates to your data.

Ken

#5 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 10 April 2006 - 10:44 PM

sorry, for my post, it should have been:

$_POST["{$row['product_id']}"]

but ken is right. check what's in $_POST first.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users