PHP Variable Parsing Order

Associate
Joined
31 Jan 2007
Posts
1,860
Hello,
I have this line of PHP:

PHP:
$formvalue_{$item}id = ${$item}_id;

I want the stuff in curly brackets ie: $item to be parsed by PHP before the rest of the line so that the value of $item makes up other variable names.

I know that the curly brackets I have used above doesn;t work but it's the clearest way of me explaining it. Any one have any ideas how to make PHP parse certain variables before others?

Thanks

Regards,
Neil
 
Parenthesis. To fix your example, which doesn't need parenthesis:
PHP:
${'formvalue_' . $item . 'id'} = ${$item . '_id'};

An example of where you might need Parenthesis:
PHP:
$var = 1 + ((2 * 1) / 5);
 
Right, In this snippet:

PHP:
$addcomment = mysql_query("INSERT INTO atm_comments (comment_name, comment_email, comment_rating, comment_comment, comment_auth, comment_".$item."id, comment_emailshow) VALUES('$formvalue_name', '$formvalue_email', '$formvalue_rating', '$formvalue_comment', '$formvalue_auth', '$formvalue_rideid', '$formvalue_emailshow')") or die(mysql_error());

Near the end I ahve $formvalue_rideid,

How can I swap the word ride to use the $item variable?

It's the whole parenthesis and quotes thing I get confuzzled with.
 
PHP:
$addcomment = mysql_query("INSERT INTO atm_comments (comment_name, comment_email, comment_rating, comment_comment, comment_auth, comment_".$item."id, comment_emailshow) VALUES('$formvalue_name', '$formvalue_email', '$formvalue_rating', '$formvalue_comment', '$formvalue_auth', '$formvalue_{$item}id', '$formvalue_emailshow')") or die(mysql_error());
 
That doesn;t seem to work as I have just tried submitting a comment which should use that query to enter it into a database table, which it does but does not assign it a ride id so thats how I know that part is not working. Any other ideas?
 
Just done that and got the following:

INSERT INTO atm_comments (comment_name, comment_email, comment_rating, comment_comment, comment_auth, comment_rideid, comment_emailshow) VALUES('name', 'email', '1star', 'comment', '0', 'rideid', '1')

As you can see it is entering the word 'rideid' as the value so how do I get it to enter the value?

In this case it is not meant to auto increment, it is just entering the id of the ride to which it is related to
 
The value of $item in this case would be "ride" so when $item has been parsed then it would make the new of a variable called $formvalue_rideid which would translate into an id number
 
right.. think I understand.. though this is majorly convoluted by the sounds of it.

PHP:
$addcomment = mysql_query("INSERT INTO atm_comments (comment_name, comment_email, comment_rating, comment_comment, comment_auth, comment_".$item."id, comment_emailshow) VALUES('$formvalue_name', '$formvalue_email', '$formvalue_rating', '$formvalue_comment', '$formvalue_auth', '" . ${'formvalue_' . $item . 'id'} . ", '$formvalue_emailshow')") or die(mysql_error());
 
Back
Top Bottom