I already have my desired data retrieved from the database and output it via the browser for web use but now I am trying to insert it in to an excel doc and email it.
I have the beginnings of the document and am able to email it as an attachment fine, it's just how to transpose the html output into the excel document that I am stuck at as I can't think of the best way to do it.
This is how I achieve my HTML output (apologies for the length):
// print table
echo '<table>';
echo '<tr><th rowspan="2">Day</th>';
foreach($typesorder as $type) {
if(in_array($type, $types)) {
echo '<th colspan="3">' . $type . '</th>';
}
}
echo '<th colspan="3">Total Conversions</th>';
echo '</tr>';
echo '<tr>';
foreach($typesorder as $type) {
if(in_array($type, $types)) {
echo '<th>Week ' . $weekstart_A_data['week'] . ' ' . $weekstart_A_data['year'] . '</th>';
echo '<th>Week ' . $weekstart_B_data['week'] . ' ' . $weekstart_B_data['year'] . '</th>';
echo '<th>+/-</th>';
}
}
// Total Conversions section
echo '<th>Week ' . $weekstart_A_data['week'] . ' ' . $weekstart_A_data['year'] . '</th>';
echo '<th>Week ' . $weekstart_B_data['week'] . ' ' . $weekstart_B_data['year'] . '</th>';
echo '<th>+/-</th>';
echo '</tr>';
foreach($dailytotals as $thedate => $data) {
$daily_conversions = 0;
$daily_conversionsB = 0;
echo '<tr>';
echo '<td>' . date('l', strtotime($thedate)) . '</td>';
foreach($typesorder as $type) {
if(in_array($type, $types)) {
$conversions = $data[$type];
$total_conversions[$type] += $conversions;
$daily_conversions += $conversions;
$week_A_conversions = $conversions;
$week_B_conversions = $dailytotalsB[$weekstartB][$type];
$total_conversionsB[$type] += $dailytotalsB[$weekstartB][$type];
$daily_conversionsB += $week_B_conversions;
$differential = $dailytotalsB[$weekstartB][$type] - $conversions;
echo '<td>'. number_format($week_A_conversions) . '</td>';
echo '<td>'. number_format($week_B_conversions) . '</td>';
if($differential < 0 ) {
$class = "class='diffred'";
} else if($differential > 0) {
$class = "class='diffblue'";
} else {
$class='';
}
// differential between Week A and Week B
echo '<td ' . $class . '>' . $differential . '</td>';
}
}
$weekstartB = date("Y-m-d", strtotime('+1 day', strtotime($weekstartB)));
$differentialtotal = $daily_conversionsB - $daily_conversions;
echo '<td>' . number_format($daily_conversions) . '</td>';
echo '<td>' . number_format($daily_conversionsB) . '</td>';
if($differentialtotal < 0 ) {
$class = "class='diffred'";
} else if($differentialtotal > 0) {
$class = "class='diffblue'";
} else {
$class='';
}
echo '<td ' . $class . '>' . $differentialtotal . '</td>';
echo '</tr>';
}
echo '<tr>';
echo '<td><strong>Total</strong></td>';
// reset both week A and B
$overall_conversions = 0;
$overall_conversionsB = 0;
foreach($typesorder as $type) {
if(in_array($type, $types)) {
$conversions = $total_conversions[$type];
$overall_conversions += $conversions;
$conversionsB = $total_conversionsB[$type];
$overall_conversionsB += $conversionsB;
echo '<th>' . number_format($conversions) . '</th>';
echo '<th>' . number_format($conversionsB) . '</th>';
echo '<th>' . number_format($conversionsB - $conversions) . '</th>';
}
}
echo '<th>' . number_format($overall_conversions) . '</th>';
echo '<th>' . number_format($overall_conversionsB) . '</th>';
echo '<th>' . number_format($overall_conversionsB - $overall_conversions) . '</th>';
echo '</tr>';
echo '</table>';
This outputs a table with the following structure:
----------------------------------------------------------------------------------
| | Type 1 | Type 2 | ... | Total Conversions |
| Day -------------------------------------------------------------------------|
| | Week 1 2013 | Week 1 2012 | ... | ... | ... | ... | ... |
|--------------------------------------------------------------------------------|
|Sunday | 135 | 143 | ... | ... | ... | ... | ... |
|--------------------------------------------------------------------------------|
| ... | ... | ... | ... | ... | ... | ... | ... |
|--------------------------------------------------------------------------------|
|Total | ... | ... | ... | ... | ... | ... | ... |
----------------------------------------------------------------------------------
Hopefully that makes some sense, the ...
are just placeholders for repeating data.
I don't need to do any formulas since I already have all the data I need but I wouldn't rule it out as it might be easier to total using PHPExcel.
I know this is a horrible question but I am genuinely stumped how to start off. I'm not expecting the full, exact answer to my specific scenario (although that would be magical) but in reality any pointers would help.
P.S. I know how to insert data into the Excel document using PHPExcel but it's transposing my table that's the problem. I think the first step is to add all the data to a multi-dimensional array instead of printing it but I will see what the responses are first.
Also, please note that I don't want to output as CSV as I wish to automatically email the pre-prepared and formatted Excel sheet.