Mpdf takes too much time to generate PDF - php

I'm using mPDF in Yii2 php framwork to generate the pdf as report.
getting the result as expected but it is taking too much time to generate the pdf-report (around 5-10 sec).
Pdf
This is the code for mPdf setup
ini_set('memory_limit', '6144M');
set_time_limit(10000);
$path = \Yii::getAlias("#vendor/MPDF/mpdf.php");
require_once($path);
$mpdf = new \mPDF('en-GB-x', '', '', '', 5, 5, 5, 5, 6, 3);
$mpdf->addPage('P');
$mpdf->packTableData = true;
$mpdf->SetDisplayMode('fullpage');
$mpdf->list_indent_first_level = 0;
$mpdf->showWatermarkText = true;
$mpdf->watermarkTextAlpha = 0.3;
$mpdf->simpleTables = true;
$file_name = '';
And this is the main code to generate PDF
$table = "<table width='100%'>";
$table .= "<thead>";
$table .= "<tr>";
$table .= "<td colspan='1' style='width: 20%;' align='left'></td>";
$table .= "<td colspan='5' style='width: 60%;' align='center'>";
$table .= "<h3 style='margin-top:20px; font-size: 26px;'>" . $ofc_detail['company_name'] . "</h3>";
$table .= "</td>";
$table .= "<td colspan='1' style='width: 40%; font-size: 12px;' align='center'>";
$table .= "<div><img src='" . $photo . "' width='31' height='50' /><br /></div>";
$table .= "</td>";
$table .= "</tr>";
$table .= "<tr >";
$table .= "<th colspan='7'><center>QUOTE</center></th>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<th colspan='7' align='left'>Date :" . $quotation_date . "</th>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<th colspan='7' align='left'>Quotation ID :" . $quotation_id . "</th>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<th colspan='7' align='left'>Created By :" . $created_by . "</th>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<th height='40' colspan='7' align='left'></th>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<th colspan='7' align='left'>To</th>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<th align='left'>Customer Name</th>";
$table .= "<td colspan='6'>" . $cust_name . "</td>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<th align='left'>Mobile </th>";
$table .= "<td colspan='6'>" . $mobile . "</td>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<th align='left'>Company Name</th>";
$table .= "<td colspan='6'>" . $organization . "</td>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<th align='left'>GST No.</th>";
$table .= "<td colspan='6'>" . $gst_no . "</td>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<th align='left'>Address</th>";
$table .= "<td colspan='6'>" . $addr_str . "</td>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<th align='left'></th>";
$table .= "<td colspan='6'>" . $addr_delivery . "</td>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<th height='60' colspan='7' align='left'></th>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<th colspan='7'><center>Sub: - " . $subject . "</center></th>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<td colspan='7'>Dear Sir,</td>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<td colspan='7'>As discussed I am sending you the quotation</td>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<th height='20' colspan='7' align='left'></th>";
$table .= "</tr>";
$table .= "</thead>";
$table .= "</table>";
$table .= "<table border='1' width='100%'>";
$table .= "<tr>";
$table .= "<th>S.No.</th>";
$table .= "<th>Item Name</th>";
$table .= "<th>MRP</th>";
$table .= "<th>Selling Price</th>";
$table .= "<th>Quantity</th>";
$table .= "<th>Discount</th>";
$table .= "<th>Amount</th>";
$table .= "</tr>";
$table .= "<tbody>";
$i = 1;
$tot = 0;
foreach ($query as $k => $v) {
$tot = $tot + $v['amt'];
if ($v['discount'] == '') {
$discount = '0';
} else {
$discount = $v['discount'];
}
$table .= "<tr>"
. "<td align='center'>" . $i++ . "</td>"
. "<td align='left'>" . $v['name'] . "</td>"
. "<td align='center'>" . $v['MRP'] . "</td>"
. "<td align='center'>" . $v['selling_price'] . "</td>"
. "<td align='center'>" . $v['qty'] . "</td>"
. "<td align='center'>" . $discount . "%</td>"
. "<td align='right'>" . $v['amt'] . "</td>"
. "</tr>";
}
$table .= "<tr>";
$table .= "<td colspan='1'></td>";
$table .= "<td colspan='5' align='right'>TOTAL</td>";
$table .= "<td colspan='1' align='right'>" . $tt_amt . "</td>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<td colspan='1'></td>";
$table .= "<td colspan='5' align='right'>DISCOUNT</td>";
$table .= "<td colspan='1' align='right'>" . $dis_amt . "</td>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<td colspan='1'></td>";
$table .= "<td colspan='5' align='right'>SUB TOTAL</td>";
$table .= "<td colspan='1' align='right'>" . $sub_amt . "</td>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<td colspan='1'></td>";
$table .= "<td colspan='5' align='right'>CGST</td>";
$table .= "<td colspan='1' align='right'>" . $cgst_amt . "</td>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<td colspan='1'></td>";
$table .= "<td colspan='5' align='right'>SGST</td>";
$table .= "<td colspan='1' align='right'>" . $sgst_amt . "</td>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<td colspan='1'></td>";
$table .= "<td colspan='5' align='right'>IGST</td>";
$table .= "<td colspan='1' align='right'>" . $igst_amt . "</td>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<td colspan='1'></td>";
$table .= "<td colspan='5' align='right'>CESS</td>";
$table .= "<td colspan='1' align='right'>" . $cess_amt . "</td>";
$table .= "</tr>";
if(!empty($queryToGetOtherItems)){
foreach ($queryToGetOtherItems as $data) {
if ($data['discount_percent'] == '') {
$discount = '0';
} else {
$discount = $data['discount_percent'];
}
$table .= "<tr>"
. "<td colspan='1'></td>"
. "<td colspan='5' align='right'>" . $data['name'] . "</td>"
. "<td colspan='1' align='right'>" . $data['total_amount'] . "</td>"
. "</tr>";
}
}
$table .= "<tr>";
$table .= "<td colspan='1'></td>";
$table .= "<td colspan='5' align='right'>GRAND TOTAL</td>";
$table .= "<td colspan='1' align='right'>" . $tot_amt . "</td>";
$table .= "</tr>";
$table .= "</tbody>";
$table .= "</table>";
$table .= "<table width='100%'>";
$table .= "</tbody>";
$table .= "<tr>";
$table .= "<th height='60' colspan='7' align='left'></th>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<td colspan='7' align='left'><strong>TERMS & CONDITIONS :</strong><br>" . $description . "</td>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<td align='left' colspan='7'><strong>With Regards</strong><br><br><strong>Name :</strong> " . $ofc_detail['company_name'] . "</td>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<td align='left' colspan='7'><strong>Mobile No</strong> :" . $ofc_detail['ofc_mob_one'] . "</td>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<th height='60' colspan='7' align='left'></th>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<td colspan='7'><center>Address : " . $ofc_detail['address'] . "</center></td>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<td colspan='7'><center>GST No : " . $ofc_detail['gst_number'] . "</center></td>";
$table .= "</tr>";
$table .= "<tr>";
$table .= "<td colspan='7'><center>E-Mail ID : " . $ofc_detail['ofc_email_one'] . "</center></td>";
$table .= "</tr>";
$table .= "</tbody>";
$table .= "</table>";
}
$file_name = "Quotation_" . $quotation_id . "_" . date('Y_m_d_his') . ".pdf";
$mpdf->WriteHTML($table);
$files = $_SERVER['DOCUMENT_ROOT'] . '/path/' . $file_name;
$content = $mpdf->Output($files, 'F');
To resolve the issue I've checked for the following this
Optimized the main query and was able to get response in just 0.26 to 0.30sec
but still not able to generate in less seconds.

Related

PHP echo without a loop. SQL Query

Here is the subject matter: Link, Click a players name to see.
I want to have the players name as a header so that the players name isn't repeated over and over again for each resulting row in the Roster table. I know how to loop through results for rows, but I only want the name once at the top and don't know how to do that. This is how I print the table code wise
<?php
$pid = $_POST['PID'];
$query = "SELECT Rosters.PID, Rosters.Goals, Rosters.Assists, Rosters.PIM, Rosters.Num, Rosters.TID, Players.pid, Players.firstname, Players.lastname,
(Rosters.Goals + Rosters.Assists) AS Points
FROM Rosters
INNER JOIN Players
ON Rosters.PID = Players.pid
WHERE Rosters.PID = $pid
ORDER BY Points DESC, Goals DESC";
$result = $conn->query($query);
$query2 = "SELECT Rosters.PID, SUM( Rosters.Goals ) Goals, SUM( Rosters.Assists ) Assists, SUM( Rosters.PIM ) PIM, Rosters.Num, Rosters.TID, Players.pid, Players.firstname, Players.lastname,
SUM((Rosters.Goals + Rosters.Assists)) AS Points
FROM Rosters
INNER JOIN Players ON Rosters.PID = Players.pid
WHERE Rosters.PID =$pid
ORDER BY Points DESC , Goals DESC";
$result2 = $conn->query($query2);
echo
if (!empty($_POST["PID"])) {
echo "<table class='stat-table-wide table sortable' align='center'>";
echo "<tr>";
echo "<th class='hover'>Season</th>";
echo "<th class='hover'>Num</th>";
echo "<th class='hover'>Player</th>";
echo "<th class='hover'>G</th>";
echo "<th class='hover'>A</th>";
echo "<th class='hover'>Pts</th>";
echo "<th class='hover'>PIM</th>";
echo "</tr>";
$i = 13;
$j = 14;
while ($row = $result->fetch_assoc()) {
$i++;
$j++;
$goals = $row["Goals"];
$assists = $row["Assists"];
$points = $goals + $assists;
echo "<tr><td>20" . $i . "-20" . $j . "</td>";
echo "<td>" . $row["Num"] . "</td>";
echo "<td>" . $row["firstname"] . " " . $row["lastname"] . "</td>";
echo "<td>" . $row["Goals"] . "</td>";
echo "<td>" . $row["Assists"] . "</td>";
echo "<td>" . $row["Points"] . "</td>";
echo "<td>" . $row["PIM"] . "</td></tr>";
}
echo "</table>";
echo "<span class='style35'>Total</span><br />";
echo "<table class='stat-table-wide table sortable' align='center'>";
echo "<tr>";
echo "<th class='hover'>Player</th>";
echo "<th class='hover'>G</th>";
echo "<th class='hover'>A</th>";
echo "<th class='hover'>Pts</th>";
echo "<th class='hover'>PIM</th>";
echo "</tr>";
while ($row = $result2->fetch_assoc()) {
$goals = $row["Goals"];
$assists = $row["Assists"];
$points = $goals + $assists;
echo "<td>" . $row["firstname"] . " " . $row["lastname"] . "</td>";
echo "<td>" . $row["Goals"] . "</td>";
echo "<td>" . $row["Assists"] . "</td>";
echo "<td>" . $row["Points"] . "</td>";
echo "<td>" . $row["PIM"] . "</td></tr>";
}
echo "</table>";
}
?>
I just want Players.firstname and Players.lastname printed once before any of the tables are generated.
Try this:
if (!empty($_POST["PID"])) {
$row = $result->fetch_assoc();
echo "<span class='style35'>" . $row["firstname"] . " " . $row["lastname"] . "</span><br />";
$result->data_seek(0);
echo "<table class='stat-table-wide table sortable' align='center'>";
echo "<tr>";
echo "<th class='hover'>Season</th>";
echo "<th class='hover'>Num</th>";
echo "<th class='hover'>G</th>";
echo "<th class='hover'>A</th>";
echo "<th class='hover'>Pts</th>";
echo "<th class='hover'>PIM</th>";
echo "</tr>";
$i = 13;
$j = 14;
while ($row = $result->fetch_assoc()) {
$i++;
$j++;
$goals = $row["Goals"];
$assists = $row["Assists"];
$points = $goals + $assists;
echo "<tr><td>20" . $i . "-20" . $j . "</td>";
echo "<td>" . $row["Num"] . "</td>";
echo "<td>" . $row["Goals"] . "</td>";
echo "<td>" . $row["Assists"] . "</td>";
echo "<td>" . $row["Points"] . "</td>";
echo "<td>" . $row["PIM"] . "</td></tr>";
}
echo "</table>";
echo "<span class='style35'>Total</span><br />";
echo "<table class='stat-table-wide table sortable' align='center'>";
echo "<tr>";
echo "<th class='hover'>Player</th>";
echo "<th class='hover'>G</th>";
echo "<th class='hover'>A</th>";
echo "<th class='hover'>Pts</th>";
echo "<th class='hover'>PIM</th>";
echo "</tr>";
while ($row = $result2->fetch_assoc()) {
$goals = $row["Goals"];
$assists = $row["Assists"];
$points = $goals + $assists;
echo "<td>" . $row["firstname"] . " " . $row["lastname"] . "</td>";
echo "<td>" . $row["Goals"] . "</td>";
echo "<td>" . $row["Assists"] . "</td>";
echo "<td>" . $row["Points"] . "</td>";
echo "<td>" . $row["PIM"] . "</td></tr>";
}
echo "</table>";
}
?>
I don't know if you wanted the name of the player in the totals, so I left it there, but you can delete it if you want. I also applied to the player name the same style as the total.
You could get the first row of the result before your loop, then just reset the cursor, and completely ignore the players name inside your loop.
$first = $result->fetch_assoc();
// print out first name
echo($first['name'] . " " . $first['surname']);
// reset cursor
$result->data_seek(0);
// do your loops and stuff.
while ($row = $result->fetch_assoc()) {
...
}
You can do like this.If you do not want to change the sql query.
$ctr = 0;
while ($row = $result->fetch_assoc()) {
$ctr++;
if($ctr == 1) {
echo "</table>";
echo "<span class='style35'>Total</span><br />";
echo "<table class='stat-table-wide table sortable' align='center'>";
echo "<tr>";
echo "<th class='hover'>Player</th>";
echo "<th class='hover'>G</th>";
echo "<th class='hover'>A</th>";
echo "<th class='hover'>Pts</th>";
echo "<th class='hover'>PIM</th>";
echo "</tr>";
echo "<tr><td>" . $row["firstname"] . " " . $row["lastname"] . "</td> </tr>";
}
$i++;
$j++;
$goals = $row["Goals"];
$assists = $row["Assists"];
$points = $goals + $assists;
echo "<tr><td>20" . $i . "-20" . $j . "</td>";
echo "<td>" . $row["Num"] . "</td>";
echo "<td>" . $row["firstname"] . " " . $row["lastname"] . "</td>";
echo "<td>" . $row["Goals"] . "</td>";
echo "<td>" . $row["Assists"] . "</td>";
echo "<td>" . $row["Points"] . "</td>";
echo "<td>" . $row["PIM"] . "</td></tr>";
}

Results from the table and link

I do not know how to add a link to the results of table.
I want to change # to
edit
Thank you in advance for your help.
foreach($results as $row) {
$table = '<tr>';
$table .= '<td>';
$table .= $row['filename'];
$table .= '#';
$table .= '</td>';
$table .= '<td class="col1 nw tar">';
$table .= $row['size'];
$table .= '</td>';
$table .= '<td class="col1 nw tar">';
$table .= $row['type'];
$table .= '</td>';
$table .= '<td class="col1 nw tar">£';
$table .= $row['caption'];
$table .= '</td>';
$table .= '</tr>';
}
In the fifth line of your code, instead of
$table .= '#';
you should write
$table .= 'edit';
foreach($results as $row) {
$table = '<tr>';
$table .= '<td>';
$table .= $row['filename'];
$table .= "<a href='photo_update.php?id={$row['id']}'>edit</a>";
$table .= '</td>';
$table .= '<td class="col1 nw tar">';
$table .= $row['size'];
$table .= '</td>';
$table .= '<td class="col1 nw tar">';
$table .= $row['type'];
$table .= '</td>';
$table .= '<td class="col1 nw tar">£';
$table .= $row['caption'];
$table .= '</td>';
$table .= '</tr>';
}

Displaying php query in column instead of row

Hey guys i have a postgres database to which i am connecting and fetching data from using php. I am a newbie to php. But anyway i have this as my query
$sql = "SELECT to_char (a.CALLDATE,'yyyymm') as month,min(a.calldate) as start_time,max(a.calldate) as end_time,
ceil(SUM (a.CALLDURATION::integer) / 60) AS minutes,
COUNT (DISTINCT a.IDENTIFIANT) AS distinct_callers,
a.zoneiddest as country_code,b.country
FROM cdr_data a,COUNTRY_CODES b
WHERE a.CALLSUBCLASS = '002'
AND a.CALLCLASS = '008'
and a.zoneiddest::integer > 0
AND SUBSTR (a.CALLEDNUMBER, 1, 2) NOT IN
('77', '78', '75', '70', '71', '41', '31', '39', '76','79')
and not substr(a.zoneiddest , 1 ,3) in ('254','255','256','211','257','250','256')
and trim(a.zoneiddest) = trim(b.country_code)
GROUP BY to_char (a.CALLDATE,'yyyymm') ,a.zoneiddest,b.country
ORDER BY 1";
And i have this to return the data that is queried:
// iterate over result set
// print each row
while ($row = pg_fetch_array($result)) {
echo "Month: " . $row[0] . "<br />";
echo "Start Time: " . $row[1] . "<br />";
echo "End Time: " . $row[2] . "<br />";
echo "Minutes: " . $row[3] . "<br />";
echo "Distinct Callers: " . $row[4] . "<br />";
echo "Country Code: " . $row[5] . "<br />";
echo "Country: " . $row[6] . "<br />";
}
This returns my data in this format
Month: 201212
Start Time: 2012-12-28 15:51:04
End Time: 2012-12-28 15:51:04
Minutes: 0
Distinct Callers: 1
Country Code: 44
Country: United Kingdom of Great Britain and Northern Ireland
But would love to have this data in the table where month, start time, end time, minutes, distinct callers, country code and country are the column headers in the table. How can i achieve this.
Please check the code below. Let me know if my understanding of your requirements is incorrect.
<table>
<tr>
<td>Month</td>
<td>Start Time</td>
<td>End Time</td>
<td>Minutes</td>
<td>Distinct Callers</td>
<td>Country Code/td>
<td>Country</td>
</tr>
<?php
while ($row = pg_fetch_array($result)) {
echo '<tr>';
echo "<td>" . $row[0] . "</td>";
echo "<td>" . $row[1] . "</td>";
echo "<td>" . $row[2] . "</td>";
echo "<td>" . $row[3] . "</td>";
echo "<td>" . $row[4] . "</td>";
echo "<td>" . $row[5] . "</td>";
echo "<td>" . $row[6] . "</td>";
echo '</tr>';
}
?>
</table>
dont forget to add loop
echo "<table cellpadding='5' cellspacing='1' style='background-color: #D7D2D2;font-size:12px;font-family:Tahoma;'>";
echo "<tr>";
echo "<th style='background-color: #FFFFFF;font-size:11px' align='left'>Month</th>";
echo "<th style='background-color: #FFFFFF;font-size:11px' align='left'>Start Time</th>";
echo "<th style='background-color: #FFFFFF;font-size:11px' align='left'>End Time</th>";
echo "<th style='background-color: #FFFFFF;font-size:11px' align='left'>Minutes</th>";
echo "<th style='background-color: #FFFFFF;font-size:11px' align='left'>Distinct Callers</th>";
echo "<th style='background-color: #FFFFFF;font-size:11px' align='left'>Country Code</th>";
echo "<th style='background-color: #FFFFFF;font-size:11px' align='left'>Country</th>";
echo "</tr>";
echo "<tr>";
echo "<td style='background-color: #FFFFFF;'>$row[0]</td>";
echo "<td style='background-color: #FFFFFF;'>$row[1]</td>";
echo "<td style='background-color: #FFFFFF;'>$row[2]</td>";
echo "<td style='background-color: #FFFFFF;'>$row[3]</td>";
echo "<td style='background-color: #FFFFFF;'>$row[4]</td>";
echo "<td style='background-color: #FFFFFF;'>$row[5]</td>";
echo "<td style='background-color: #FFFFFF;'>$row[6]</td>";
echo "</tr>";
echo "</table>";
You can do it like this
$output = "<table>";
$output .= "<tr>";
$output .= "<td> Month </td>";
$output .= "<td> Start Time </td>";
$output .= "<td> End Time </td>";
$output .= "<td> Minutes </td>";
$output .= "<td> Distinct Callers </td>";
$output .= "<td> Country Codes </td>";
$output .= "<td> Country </td>";
$output .= "</tr>";
while ($row = pg_fetch_array($result)) {
$output .= "<tr>";
$output .= "<td>". $row[0] . "</td>";
$output .= "<td>". $row[1] . "</td>";
$output .= "<td>". $row[2] . "</td>";
$output .= "<td>". $row[3] . "</td>";
$output .= "<td>". $row[4] . "</td>";
$output .= "<td>". $row[5] . "</td>";
$output .= "<td>". $row[6] . "</td>";
$output .= "</tr>";
}
$output .= "</table>";
echo $output;
Hope this helps
echo "<table>";
echo "<tr>";
echo "<td>Month</td>";
echo "<td>Start Time</td>";
echo "<td>End Time</td>";
echo "<td>Minutes</td>";
echo "<td>Distinct Callers</td>";
echo "<td>Country Code</td>";
echo "<td>Country</td>";
echo "</tr>";
while ($row = pg_fetch_array($result))
{
echo "<tr>";
echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "<td>".$row[3]."</td>";
echo "<td>".$row[4]."</td>";
echo "<td>".$row[5]."</td>";
echo "<td>".$row[6]."</td>";
echo "</tr>";
}
echo "</table>";

send dynamic table via email

I am working on this web application that needs to send a certain report. The report is dynamic however and it is in a table format. The thing i hope to do now is to email the whole table via email to someone. Is there any way I can do so?
php script
<?php
$con = mysql_connect("localhost", "root", "");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("mydb", $con);
$input = $_POST['id'];
$query = mysql_query("SELECT * FROM table WHERE ID = '$input'");
echo "<table border='5' align=center>
<tr>
<th>ID</th>
<th>Type</th>
<th>Setting</th>
<th>Value</th>
<th>Actual</th>
</tr>";
while($row = mysql_fetch_array($query))
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['Type'] . "</td>";
echo "<td>" . $row['Setting'] . "</td>";
echo "<td>" . $row['Value'] . "</td>";
echo "<td>" . $row['Actual'] . "</td>";
echo "</tr>";
if($row['Value'] != $row['Actual'])
{
echo "<td bgcolor='#FD2911'>" . "X" . "</td>";
}
else
{
echo "<td bgcolor='#1CDC15'>" . "O" . "</td>";
}
}
echo "<br>";
mysql_close($con);
?>
html code
<form action="query1.php" method="POST">
Enter the choice: <input type="varchar" name="id" /><br />
<input type="submit" value="Audit" />
</form>
You concatenate the whole table:
$table = '';
$table .= "<table border='5' align=center>
<tr>
<th>ID</th>
<th>Type</th>
<th>Setting</th>
<th>Value</th>
<th>Actual</th>
</tr>";
while($row = mysql_fetch_array($query))
{
$table .= "<tr>";
$table .= "<td>" . $row['ID'] . "</td>";
$table .= "<td>" . $row['Type'] . "</td>";
$table .= "<td>" . $row['Setting'] . "</td>";
$table .= "<td>" . $row['Value'] . "</td>";
$table .= "<td>" . $row['Actual'] . "</td>";
$table .= "</tr>";
if($row['Value'] != $row['Actual'])
{
$table .= "<td bgcolor='#FD2911'>" . "X" . "</td>";
}
else
{
$table .= "<td bgcolor='#1CDC15'>" . "O" . "</td>";
}
}
$table .= "</table><br>";
mail('to#example.com', 'My Subject', $table);
if you are wanting to do more than the table, you could use output buffering;
<?php
ob_start();
# all your code/page/table(s) with echos and all
$toBeSent = ob_get_contents();
ob_end_clean();
?>
then send the entire contents between ob_start and ob_end_clean via email by replacing $table in the mail() line of Mihai's post with $toBeSent.

Foreach function to start from the 2nd value in the array

How can I slice away the first value?
foreach ($dataArray ['simpleforecast']['forecastday'] as $arr) {
$html .= "<td align='center' style='font-size:10px; font-weight:bold' >" . substr($arr['date']['weekday'], 0,3) . "<br />";
$html .= "<img style='border-radius:15px' src='http://icons-pe.wxug.com/i/c/a/" . $arr['icon'] . ".gif' border=0 /><br />";
$html .= "<font style='font-weight:bold' color='#555'>" . $arr['high'][$tempScale] . $tempUnit . " </font>";
$html .= "<font style='font-weight:normal' color='grey'>" . $arr['low'][$tempScale] . $tempUnit . "</font>";
$html .= "</td>";
}
$first = array_shift($dataArray ['simpleforecast']['forecastday']);
// use your loop
Quick and dirty:
$first = true;
foreach ($dataArray['simpleforecast']['forecastday'] as $arr) {
if ($first) { $first = !$first; continue; }
$html .= "<td align='center' style='font-size:10px; font-weight:bold' >" . substr($arr['date']['weekday'], 0,3) . "<br />";
$html .= "<img style='border-radius:15px' src='http://icons-pe.wxug.com/i/c/a/" . $arr['icon'] . ".gif' border=0 /><br />";
$html .= "<font style='font-weight:bold' color='#555'>" . $arr['high'][$tempScale] . $tempUnit . " </font>";
$html .= "<font style='font-weight:normal' color='grey'>" . $arr['low'][$tempScale] . $tempUnit . "</font>";
$html .= "</td>";
}
$i = 1;
foreach ($dataArray ['simpleforecast']['forecastday'] as $arr) {
if($i==1)
{
$i++;
continue;
}
$html .= "<td align='center' style='font-size:10px; font-weight:bold' >" . substr($arr['date']['weekday'], 0,3) . "<br />";
$html .= "<img style='border-radius:15px' src='http://icons-pe.wxug.com/i/c/a/" . $arr['icon'] . ".gif' border=0 /><br />";
$html .= "<font style='font-weight:bold' color='#555'>" . $arr['high'][$tempScale] . $tempUnit . " </font>";
$html .= "<font style='font-weight:normal' color='grey'>" . $arr['low'][$tempScale] . $tempUnit . "</font>";
$html .= "</td>";
}
if(key($dataArray) === 0)) continue; But this will work only if you do not specify keys manually.

Categories