StuartBombay
Programmer
I used code authored by jpadie that I found on this forum, but I can't find the thread again.
I took his code and made a function, and created a page to send a request to the function. It works, accept the spreadsheet that opens also has the HTML header and footer in it. All I want are the results of the query.
The problem lies in my unclear understanding, but I've tried such things as adding a header that relocates to a blank page but I end up with, well, a blank page!
If someone could point out my error I would appreciate it!
The function:
The call:
I took his code and made a function, and created a page to send a request to the function. It works, accept the spreadsheet that opens also has the HTML header and footer in it. All I want are the results of the query.
The problem lies in my unclear understanding, but I've tried such things as adding a header that relocates to a blank page but I end up with, well, a blank page!
If someone could point out my error I would appreciate it!
The function:
Code:
function excel_output($table) {
global $conn;
echo $table;
$file = "test.csv";
$temp_str = "";
$querystring = "SELECT * from " . $table ."\n";
$result_csv=mysqli_query($conn, $querystring);
$fh=fopen($file,"w+") or die ("unable to open file"); //note that this APPENDS not overwrites
while($row=mysqli_fetch_assoc($result_csv)):
foreach ($row as &$val):
$val = str_replace('"', "'", $val);
endforeach;
fputcsv($fh, $row, ",", '"');
$temp_str .= '"'.implode('","',$row).'"\n\r';//used for the non file based download
endwhile;
fclose($fh);
header("Content-disposition: attachment; filename=$file");
header("Content-Type: application/force-download");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($file));
header("Pragma: no-cache");
header("Expires: 0");
readfile($file);
}
The call:
Code:
<?php
$page_title = 'PPS Site Summary ';
include('header.html');
include('functions/form_functions.inc');
require('../../includes/configdb.inc');
require('../../includes/opendb.inc');
$display_block = "";
$display_name = "<h2>Export Stuff</h2><br>";
global $conn;
if (!$_POST) {
//if a selection has not yet been made then show the selection form
$display_block .= "
<form method=\"POST\" action\"".$_SERVER["PHP_SELF"]."\">
Export:
<Select name=\"view\">
<Option value=\"v_program_admin_asst\">Vice Principals</option>
<Option value=\"v_program_admin\" selected=\"selected\">Principals</option>
<Option value=\"v_program_details\">Departments</option>
<Option value=\"v_program_stc\">STCs</option>
</Select>
  
<input type=\"submit\" name=\"submit\" value=\"Export\" />
</form>";
$display_block .="</h2>";
} else if ($_POST) {
//find out how to redirect to a new page so headers don't show on the spreadsheet
//After a selection has been made open the spreadsheet
$result = excel_output($_POST['view']);
} //end else if
echo $page_title = $display_name;
echo $display_block;
include('footer.html');
//Close the connection
require('../../includes/closedb.inc');
?>