I'm not sure that will work. I using templates, and not imbedded php within an html file. Are you saying that at the end of each <?php block, all the handles are closed?
Here is the basic code:
<?php
$root_path = './';
include($root_path . 'common.php');
session_pagestart("dump.php"); // handle session management and login
include_once($root_path . 'includes/db.php'); // make database connection
// Start output of page
$page_title = $lang['Data_dump'];
include($root_path . 'includes/page_header.php');
if (isset($HTTP_POST_VARS['studyid']) || isset($HTTP_GET_VARS['studyid'])) {
$study_id = isset($HTTP_POST_VARS['studyid']) ? $HTTP_POST_VARS['studyid']
: $HTTP_GET_VARS['studyid'];
$template->set_file('t_dump_result', 'data_dump_result_body.tpl');
$template->set_var(array(
'DUMP_SUBMITTED' => sprintf($lang['Dump_submitted'], $study_id, $_SESSION['email'])
));
$template->pparse('t_output', 't_dump_result');
include($root_path . 'includes/page_tail.php');
// make the database connection
include_once('adodb/toexport.inc.php');
$sql = "SELECT v.*, s.subject_id, s.date_drawn, s.date_last_modified
FROM vial v, sample s
WHERE v.sample_id = s.sample_id
AND study_id = '$study_id'";
$rs = $con->Execute($sql);
// get the temporary filename
$csv_file = get_temp_name($_SESSION['user_name'], $_SESSION['database']);
$csv_file .= '.csv';
$fp = fopen($csv_file, "w"); // open the output file
rs2csvfile($rs, $fp);
fclose($fp); // close the file
// e-mail notification to user
preg_match('/(.*)\.(.*)\.(.*).csv$/', $csv_file, $match);
list(,,,$base_name) = $match;
$message = 'Report is available at [url]https://[/url]'
. $HTTP_SERVER_VARS["HTTP_HOST"]
. '/download.php/?do='
. $_SESSION['user_name'] . '.' . $base_name;
send_mail($_SESSION['email'], 'Data Dump', $message);
}
else {
// display page where user make choice
$form_action = append_sid('data_dump.php');
$template->set_file('t_dump', 'data_dump_body.tpl');
$template->set_var(array(
'INSTRUCTIONS' => $lang['Dump_instructions'],
'L_STUDY_ID' => $lang['Study_ID'],
'L_RUN_DUMP' => $lang['Run_dump'],
'DUMP_TITLE' => $lang['Data_dump'],
'FORM_ACTION' => $form_action
));;
// Generate the page
$template->pparse( 't_output', 't_dump' );
include($root_path . 'includes/page_tail.php');
}
?>