SOLVED:
Had to put an initial value of ' ' for $rfield as hinted by KNaito BEFORE the loop (i.e. $rfield = ' ';). Thank you!
ORIGINAL POST:
As a continuation of this problem, which was solved, a new problem arises where the output of the foreach loop is duplicated in one instance but not another:
Here is my loop:
foreach($fieldsarray as $field) {
${$field} = $globalrow[$field];
if(!empty($$field)) {$rfield.='<tr><td width="50%">'.${$field.'_label'}.': </td><td width="50%">'.${$field}.'</td></tr>';
}
}
$allrows=$rfield;
The $fieldsarray has too many elements to insert here, but it is like this:
$fieldsarray = array ('firstname','lastname','email');
$globalrow is a mysql query which fetches the data of the logged in user per mysql_fetch_assoc.
The expected HTML output will be a registration confirmation with a summary of the entered data shown on screen and also in the email confirmation which the user receives.
It is a table consisting of rows of the entered data. All data is saved in the DB.
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 duplicated:
First Name: John
Last Name : Smith
First Name: John
Last Name : Smith
In both cases I simply echo them with:
<?php echo $allrows ?>
As I said, I have read about the problems of duplicate output when using while and foreach with e.g. mysql_fetch_array - but as you can see this is not the case here, and why would this work fine on screen while in the email confirmation all elements are shown twice?
EDIT TO EXPLAIN TO USER COMMENtING BELOW:
I have a registration Form, where people register (insert personal data). When they fill out the form, they get to the summary page which shows their inserted data, like:
Prefix: Mr.
First Name: John
Last Name: Smith
After klicking "Submit" - the user is marked as "registered" and he receives an email confirmation.
The Email Confirmation itself is a simple HTML File with the addition of the PHP snippet which prints the rows with the User Data (exactly as shown on the Summary page).
Now, on the summary page, the output is OK, but in the email confirmation all data is DUPLICATED, and shown, eg. as:
Prefix: Mr.
First Name: John
Last Name: Smith
Prefix: Mr.
First Name: John
Last Name: Smith
So, why would this happen? Both Summary and Confirmation HTML is absolutely the same code.
EDIT (PRINT_R):
print_r($globalrow):
Array ( [regid] => 630 [eid] => 1 [guid] => xxxxxx-D898-CBE0-D018-xxxxxxx [regcomp] => 1 [regdate] => 2014-04-25 14:57:28 [prefix] => Mr.
[nametitle] => [firstname] => Xxxxx [lastname] => Xxxxxx [company] => XXXXXXX [department] => [jobtitle] => [street] => XXXXXXXX [zip] => 8217878979 [city] => XXXXX [country] => XXXXX [priv_street] => [priv_zip] => [priv_city] =>
[priv_country] => [phone] => XXXXXX [fax] => [mobile] => [email] => XXXXXXX [travel] => [terms] => [notes] => [accommodation] => [roompartner] => [arrivaldate] => [arrivaltype] => [arrivalairport] => [arrivalflightno] =>
[arrivaltransfer] => [departuredate] => [departuretype] => [departureairport] => [departureflightno] => [departuretransfer] =>
[dinner] => [dinnerpersons] => 0 [food] => [hotel] => [checkin] => [checkout] => [roomtype] => [special] => [hotelnotes] => [groupparticipanttype] => Companion [mainparticipantid] => 629 )
print_r($fieldsarray):
Array ( [0] => prefix [1] => nametitle [2] => firstname [3] => lastname [4] => company [5] => department [6] => jobtitle [7] => email
[8] => phone [9] => fax [10] => mobile [11] => street [12] => zip [13] => city [14] => country [15] => dinner [16] => dinnerpersons
[17] => food [18] => checkin [19] => checkout [20] => hotel [21] => roomtype [22] => roompartner [23] => special [24] => hotelnotes
[25] => accommodation [26] => arrivaldate [27] => departuredate [28] => notes [29] => terms )
$field[0]['firstname'];
– Justin E Apr 25 at 12:40$$
useextract
– Justin E Apr 25 at 12:41