I have a project in which I generate a report from a database and print it out into a table on the screen. I want to make it possible for the user to email the report that was just generated to someone else. I thought I could re-create the table in the $message of the email. Unfortunately I haven’t been able to get the variables/arrays to output the values. I have attached a screenshot of the report to show how I want it to look in the email.
Here is a sample of what I am trying to do:
and where do you populate this variable within this part of the script with actual data? you’ve got to populate it the same way you’ve populated the original report.
if you don’t know how, then showing us the part of the script that deals with the report might be best - it may also be useful if you could explain us your overall setting and how the script is designed so far (both times with regards to the report output).
Yes.
You will need to preform the query within there. You are basically trying to tell php to give you variables that only exist inside a loop outside a loop. So beamPullxxx does not exist in PHP’s eyes when you called them there.
I am still a beginner at php and mysql so I am not sure I totally understand what you are saying. Are you saying I need to run the same query within the $message variable for sending the email?
Let me try to explain the project better. I have a form with 2 drop down menus. The first is start date and the 2nd is End date. The user selects the start and end date for the report they want to run. Next they click the “Get Report” Button and the report is echoed on the page below. What I want to do is add the functionality to allow the user to email the report that is now on the screen to their superior.
Here is the code for my query:
$conn = mysql_connect("localhost","user","password") or trigger_error("SQL", E_USER_ERROR);
$db = mysql_select_db("database", $conn) or trigger_error("SQL", E_USER_ERROR);
//query of min, max and avg for wire gauge 2, 3, 4 and 4.5 with endType Loop
$sql="SELECT wireGauge, MIN(beamPull) AS `min`, MAX(beamPull)AS `max`, AVG(beamPull) AS `avg`
FROM welded_wire_ties
WHERE time between '$time1' and '$time2'
AND endType = 'Loop'
GROUP BY wireGauge";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
while($row = mysql_fetch_array($result)) {
$beamPullmin[$row['wireGauge']] = $row['min'];
$beamPullmax[$row['wireGauge']] = $row['max'];
$beamPullavg[$row['wireGauge']] = $row['avg'];
}
Here is the code to output the report (I am just showing part of the table since it is large and redundant in many parts):
So would I wrap this code around the table that outputs the data and then put the $output variable in the message of the email such as $message = “$output”;?
I actually tried that and it didn’t work. Any thoughts on what I did wrong?
I actually have the echo $output working. It echoes the table and all data on the page. When I try to put the variable of $output into the message part of my email and test it I get a blank email without the table and the data. Can you see anything wrong with how my mailer is working? Here is my code:
I tested my $_POST array and got the following:
Array
(
[sendmail] => Email Report
)
When you say “Variable is not set” are you talking about $message?
To explain what happens: I have a form with 2 drop downs “time1” and “time2” (start date and end date) Once the user select the start and end date for the report they click “Get Report” (named “send_data1”). If there are no validation errors the report is printed out as well as the “Email Reports” button (named “sendmail”). The problem I am having is getting the email to contain the exact table that was on the screen.
My current code looks like this:
if (isset($_POST['send_data1'])){
$time1=$_POST['time1'];
$time2=$_POST['time2'];
$error=NULL;
// validation of time1
if ($_POST['time1'] == 'null') {
$time1 = FALSE;
$error = "<span class='red>Please select a Start Date.</span>";
}
// validation of time2
else if ($_POST['time2'] == 'null') {
$time2 = FALSE;
$error = "<span class='red>Please select an End Date.</span>";
}
if(is_null($error)){
$wireGauge=$_POST['wireGauge'];
$time1=$_POST['time1'];
$time2=$_POST['time2'];
$conn = mysql_connect("localhost","user","password") or trigger_error("SQL", E_USER_ERROR);
$db = mysql_select_db("database", $conn) or trigger_error("SQL", E_USER_ERROR);
//query of min, max and avg for wire gauge 2, 3, 4 and 4.5 with endType Loop
$sql="SELECT wireGauge, MIN(beamPull) AS `min`, MAX(beamPull)AS `max`, AVG(beamPull) AS `avg`
FROM welded_wire_ties
WHERE time between '$time1' and '$time2'
AND endType = 'Loop'
GROUP BY wireGauge";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
while($row = mysql_fetch_array($result)) {
$beamPullmin[$row['wireGauge']] = $row['min'];
$beamPullmax[$row['wireGauge']] = $row['max'];
$beamPullavg[$row['wireGauge']] = $row['avg'];
}
ob_start();
echo ("<h5>Start Date: " . date('F j, Y',strtotime($time1)) . " || End Date: " . date('F j, Y',strtotime($time2)) . "</h5>");
echo ("<table width='950' cellpadding='2' cellspacing='1' border='1' class='table'>");
echo ("<tr>");
echo ("<td>");
echo $beamPullmin['2.0'];
echo ("</td>");
echo ("<td>");
echo $beamPullmax['2.0'];
echo ("</td>");
echo ("<td>");
echo $beamPullavg['2.0'];
echo ("</td>");
echo ("</tr>");
echo ("<tr>");
echo ("<td>");
echo $beamPullmin['3.0'];
echo ("</td>");
echo ("<td>");
echo $beamPullmax['3.0'];
echo ("</td>");
echo ("<td>");
echo $beamPullavg['3.0'];
echo ("</td>");
echo ("</tr>");
echo ("</table>");
echo ("<form action=\\"reports-email2.php\\" method=\\"post\\" name=\\"Email-report\\" id=\\"Email-report\\">");
echo ("<table width=\\"350\\" border=\\"0\\" cellspacing=\\"1\\" cellpadding=\\"2\\">");
echo ("<tr>");
echo ("<td width=\\"100\\"><input type=\\"submit\\" name=\\"send_report\\" value=\\"Email Report\\"></td>");
echo ("</tr>");
echo ("</table>");
echo ("</form>");
$output=ob_get_contents();
ob_end_clean();
echo $output;
}
}
if (isset($_POST['sendmail'])){
$message = $output;
$to = "email@address.com";
$headers .= "X-Mailer: PHP4\
" ; // mailer
$headers .= "X-Priority: 3\
" ; // priority
$headers .= "Return-Path: $to\
";
$headers .= "Origin: ".$_SERVER['REMOTE_ADDR']."\
";
$headers .= "Content-Type: text/html; charset=iso-8859-1\
";
mail("email@address.com", "QC Reports for $time1 to $time2", $message,$headers);
}
in a case you press:
send_data1 the dates will be countered (maybe)
Then you have another form and another submit button: sendmail
These two forms don’t have a union. And you might don’t save the entered date1 and date2 back to the form => the form values then erased on the next submit when you press email.
If i will have a problem with my projects, i would post the relevant codes to the others who would like to help , or i give a test link where we can see how your form outputted.
If you would like to see the form in action here is a link to it: http://www.mccawphotographics.com/qc/reports-email4.php I provided the code that I thought was relevant in my last post. I have also attached the entire php file.
I agree that the two forms don’t have a union. But being very new to php I don’t yet understand how make the two forms have a union. Any help in figuring this out would be greatly appreciated.
So what would it be do:
if you select a time1 and time2 this selects the report, and you will see the Email report button. If you press you will repeat the previous post with the exact data, but in a hidden field you tell the processor program to send the email too.