EDIT: SOLVED
ORIGINAL POST:
I have a foreach loop which creates variables in the form of:
foreach ($fieldsarray as $field)
{
${$field} = $globalrow[$field];
}
This creates variables using array elements, like $firstname, $lastname, etc.
Now, since the values of these variables are fetched from the DB, I cannot use the variable names again in the same foreach. What I would like to do is to use this in the loop:
if(!empty($$field))
{
$r.$field='<tr><td width="50%">'.${$field}._label.': </td>
<td width="50%">'.${$field}.'</td></tr>';
}
Basically, this would generate table rows with data from DB, but only if the value of the variable is not empty.
My problem is: Since I have declared above that e.g. $firstname = 'John' - then the second part of the code does not work anymore, because I assume PHP does not transform the above snippet into
if(!empty($firstname))
but rather into
if(!empty(John))
How can I "force" PHP to again use the variable NAME rather than its value in this case?
Thank you for your help.
EDIT: OK, cheezeburgers suggestions worked and I think now it's working as intended. Thanks to all of you for your help.
EDIT 2: The table rows are output correctly on the screen, e.g.:
First Name: John
Last Name : Smith
but for some weird reason in the email confirmation the output is doubled:
First Name: John
Last Name : Smith
First Name: John
Last Name : Smith
In both cases I simply echo them with:
<?php echo $allrows ?>
where $allrows is in the foreach loop.
(I will post this part as a new question since this seems to be a new problem now)
if (!empty($globalrow[$field]))
? – Barmarempty($$field)
intoempty($firstname)
when$field = 'firstname'
. – Barmar