I have a 'searchresults.php' page which displays the results from a keyword search. The results are from a Dreamweaver recordset which itself is made from a mysql query (if that makes sense). The page 'searchresults.php' has an 'export to csv' button which when pressed activates the 'export_excel.php' page.
This is the code for the 'export_excel.php' page:
<?php require_once('Connections/conn_OSR.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$colname2_rsSearchResults = "-1";
if (isset($GET['site_name'])) {
$colname2_rsSearchResults = $GET['site_name'];
}
$colname4_rsSearchResults = "-1";
if (isset($GET['project'])) {
$colname4_rsSearchResults = $GET['project'];
}
$colname3_rsSearchResults = "-1";
if (isset($GET['job_no'])) {
$colname3_rsSearchResults = $GET['job_no'];
}
$colname_rsSearchResults = "-1";
if (isset($SESSION['MM_Username'])) {
$colname_rsSearchResults = $SESSION['MM_Username'];
}
mysql_select_db($database_conn_OSR, $conn_OSR);
$query_rsSearchResults = sprintf("SELECT * FROM joinversion5 WHERE site_name = %s AND username = %s OR job_no = %s AND username = %s OR project LIKE %s AND username = %s", GetSQLValueString($colname2_rsSearchResults, "text"),GetSQLValueString($colname_rsSearchResults, "text"),GetSQLValueString($colname3_rsSearchResults, "text"),GetSQLValueString($colname_rsSearchResults, "text"),GetSQLValueString("%" . $colname4_rsSearchResults . "%", "text"),GetSQLValueString($colname_rsSearchResults, "text"));
$rsSearchResults = mysql_query($query_rsSearchResults, $conn_OSR) or die(mysql_error());
$row_rsSearchResults = mysql_fetch_assoc($rsSearchResults);
$totalRows_rsSearchResults = mysql_num_rows($rsSearchResults);
// Get data records from table.
$result=mysql_query("select * from name_list order by id asc");
// Functions for export to excel.
function xlsBOF() {
echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
return;
}
function xlsEOF() {
echo pack("ss", 0x0A, 0x00);
return;
}
function xlsWriteNumber($Row, $Col, $Value) {
echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
echo pack("d", $Value);
return;
}
function xlsWriteLabel($Row, $Col, $Value ) {
$L = strlen($Value);
echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
echo $Value;
return;
}
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
header("Content-Disposition: attachment;filename=orderlist.xls ");
header("Content-Transfer-Encoding: binary ");
xlsBOF();
/
Make a top line on your excel sheet at line 1 (starting at 0).
The first number is the row number and the second number is the column, both are start at '0'
/
xlsWriteLabel(0,0,"Search Results");
// Make column labels. (at line 3)
xlsWriteLabel(2,0,"Site Name");
xlsWriteLabel(2,1,"Job Number");
$xlsRow = 3;
// Put data records from mysql by while loop.
while($row=mysql_fetch_array($result)){
xlsWriteNumber($xlsRow,0,$row[site_name]);
xlsWriteLabel($xlsRow,1,$row['job_no']);
$xlsRow++;
}
xlsEOF();
exit();
mysql_free_result($rsSearchResults);
?>
The lines in green are the column names that i've added which I want to display on the .csv file. I need to amend the line highlighted in orange so that it exports the results of the search. I've played around with the existing SQL statement to export selected columns of data, but as this is the results of a user's query, I can't work out how to capture the actual search results.???