Hi,
I have a for loop like below
for ($x=0; $x<$ParticiDetails; $x++){
$empDetails= "<tr height='20'>
<td>".$empname."</td>
<td>".$designation."</td>
<td>".$email."</td>
</tr>";
echo $empDetails
}
eg: $ParticiDetails=4, then $empDetails will print 4 times… But the problem is it will replace all the first 3 values and only will show final value, I need to store all the loop values into variable and all these variable finally store into another variable
Hi salimali,
Create a variable with an empty string outside of your loop, and then you can append the new strings to it on each iteration to build up the final output:
$empDetails = '';
for ($x = 0; $x < $ParticiDetails; $x++) {
$empDetails .= "<tr height='20'>
<td>".$empname."</td>
<td>".$designation."</td>
<td>".$email."</td>
</tr>";
}
echo $empDetails;
Hi fretburner, Thanks for your time,
I need to print all these values at a time… So if i did this, i will get the last loop value only
i have a mail function and a variable $message , These values should come in this $message = $empDetails . But i am getting only last loop only
Using fretburner’s method the $empDetails variable would contain all of the values from the loop, not just the last one…
But while the mail is sending only last value is getting
Without seeing more of your code, it’s difficult to help as I’m not sure what you’re trying to achieve.
You do realise that your loop is going to give you x identical copies of the table row, as you’re not changing the values of $empname
, $designation
and $email
?
I have like below
$message= “<html><head><title>HTML email</title></head>
<body style=‘margin-top:0px;padding-top:0px;font-family:Tahoma, Geneva, sans-serif;font-size:12px;color:#000 ;background-color:#E8E8E8 ;margin-bottom:0px;padding-bottom:0px;’>
<table cellpadding=‘0’ cellspacing=‘0’ width=‘100%’ bgcolor=‘#E8E8E8 ’ align=‘center’>”$empDetails"</table>
for example $x=2 , So the above will be like below
$message= "<html><head><title>HTML email</title></head>
<body style=‘margin-top:0px;padding-top:0px;font-family:Tahoma, Geneva, sans-serif;font-size:12px;color:#000 ;background-color:#E8E8E8 ;margin-bottom:0px;padding-bottom:0px;’>
<table cellpadding=‘0’ cellspacing=‘0’ width=‘100%’ bgcolor=‘#E8E8E8 ’ align=‘center’>
<tr height=‘20’>
<td>Employee name 2</td>
<td>Designation 2</td>
<td>Email 2</td>
</tr>
</table>
But i need like below
$message= “<html><head><title>HTML email</title></head>
<body style=‘margin-top:0px;padding-top:0px;font-family:Tahoma, Geneva, sans-serif;font-size:12px;color:#000 ;background-color:#E8E8E8 ;margin-bottom:0px;padding-bottom:0px;’>
<table cellpadding=‘0’ cellspacing=‘0’ width=‘100%’ bgcolor=‘#E8E8E8 ’ align=‘center’>
<tr height=‘20’>
<td>Employee name 1</td>
<td>Designation 1</td>
<td>Email 1</td>
</tr>”<tr height=‘20’>
<td>Employee name 2</td>
<td>Designation 2</td>
<td>Email 2</td>
</tr>"
</table>
OK, so where do the values for $empname, $designation and $email come from? Are you getting those from a database?
No, It’s from a form page which is in a JS, $empname, $designation and $email, These are the text field’s values from that form, And $x is the number of reaped that fields
For example, If $x=1, then it will be look like below (x=0,1)
<form>
Employee name : <input id=“empname” type=“text” name=“empname”><br/>
Designation : <input id="designation " type=“text” name="designation "><br/>
Email: <input id=“email” type=“text” name=“email”><br/>
Employee name : <input id=“empname” type=“text” name=“empname”><br/>
Designation : <input id="designation " type=“text” name="designation "><br/>
Email: <input id=“email” type=“text” name=“email”><br/>
</form>
salimali:
<form>
Employee name : <input id=“empname” type=“text” name=“empname”><br/>
Designation : <input id="designation " type=“text” name="designation "><br/>
Email: <input id=“email” type=“text” name=“email”><br/>
Employee name : <input id=“empname” type=“text” name=“empname”><br/>
Designation : <input id="designation " type=“text” name="designation "><br/>
Email: <input id=“email” type=“text” name=“email”><br/>
</form>
You can’t have multiple inputs with the same name, you either have to give them unique names or you can append square brackets ( ) to the name and PHP will create an array for you:
Employee name : <input id="empname" type="text" name="empname[]"><br/>
Designation : <input id="designation " type="text" name="designation[]"><br/>
Email: <input id="email" type="text" name="email"><br/>
Employee name : <input id="empname" type="text" name="empname[]"><br/>
Designation : <input id="designation " type="text" name="designation[]"><br/>
Email: <input id="email" type="text" name="email"><br/>
$employeeNames = $_POST['empname'];
// $employeeNames is now an array of names that you can loop over
Yea it’s like this
$empname=$_REQUEST['empname'];
$designation=$_REQUEST['designation'];
$email=$_REQUEST['email'];
Well, if you’re getting your form inputs as arrays, then you can do something like this:
$empDetails = '';
for ($x = 0; $x < $ParticiDetails; $x++) {
$empDetails .= "<tr height='20'>
<td>".$empname[$x]."</td>
<td>".$designation[$x]."</td>
<td>".$email[$x]."</td>
</tr>";
}
Then you can output $empDetails within your HTML template, and it will output a table row for each employee.
Thank you so much fretburner, It works