How to export query results from a table to csv?

Sauron

New Member
So I have several pages that query our DB and then present the results in a large table on the webpage. There is also a button that allows the user to export the results to a csv file. This first code snippet works perfectly well. But a couple of the other pages do not work and open a csv file with nothing in it. I got it working briefly last week, but then it stopped working again. When I got it working it appeared to be a formatting inconsistency, which explains why it would work on one page and not another. Also due to this, I do not believe the problem exists in the js that exports itSo here is the working snippet:\[code\]</div></td><td ><div id="layer2"><p class="bodytext"><?php //select the table $query = "SELECT * FROM CEP_T ORDER BY Cep_ID, Student_ID"; $result = mysql_query($query); $numberOfCeps = mysql_numrows($result);?> <html> <head> <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"> <script type="text/javascript" src="http://stackoverflow.com/questions/15796518/myJavaScripts.js"></script> <script type="text/javascript" src="http://stackoverflow.com/questions/15796518/tableSort.js"></script> <title>Display CEPs Info...</title> <link REL=StyleSheet HREF="http://stackoverflow.com/questions/15796518/formatStyles.css" TYPE="text/css" MEDIA=screen,print> </head> <body> <h3>Display <?php echo $numberOfCeps; ?> CEP designees:</h3> <div style="text-align: left; font-family: Arial;"><small><big><big> <!-- <span style="font-family: Helvetica,Arial,sans-serif;"><a href="http://stackoverflow.com/questions/15796518/reports.php">Back To Reports!</a> </span> --><form action="getCSV.php" method ="post" > <input type="hidden" name="csv_text" id="csv_text"><input type="submit" value="http://stackoverflow.com/questions/15796518/Get CSV File" onclick="getCSVData()"></form><script type="text/javascript" src="http://stackoverflow.com/questions/15796518/javaExport.js"></script><script type="text/javascript">function getCSVData(){ var csv_value=http://stackoverflow.com/questions/15796518/$('#displayAllCeps').table2CSV({delivery:'value'}); $("#csv_text").val(csv_value); }</script><div style="text-align: left; font-family: Arial;"><small><big> <?php (then it goes on to define the headers and place the results in table cells)\[/code\]And here is the code that is not working:\[code\] </div></td><td><div id="layer2"><p class="bodytext"><?php $contactType = $_GET['contactType']; $company = $_GET['company']; $endingCreateDate=''; $beginningCreateDate=''; $beginningCreateDate = date("Y-m-d",strtotime($_GET['beginningCreateDate'])); $endingCreateDate = date("Y-m-d",strtotime($_GET['endingCreateDate'])); $filterwhere=""; $filterfrom=""; if($contactType!=""){ $filterwhere=" AND ct.Contact_Type_ID=$contactType ".$filterwhere; //$filterfrom=", CONTACT_T".$filterfrom; } if($beginningCreateDate!="1969-12-31"){ $filterwhere=" AND Date_Created > '$beginningCreateDate' ".$filterwhere; //$filterfrom=", CONTACT_T".$filterfrom; } if($endingCreateDate!="1969-12-31"){ $filterwhere=" AND Date_Created < '$endingCreateDate' ".$filterwhere; //$filterfrom=", CONTACT_T".$filterfrom; } if($company!=""){ $filterwhere=" AND Individual_Company='$company' ".$filterwhere; //$filterfrom=", INDIVIDUAL_WORK_INFO_T".$filterfrom; } $query = "SELECT i.Individual_ID,First_Name,Last_Name,Email_Address, Address1,Address2,City,State,Postal_Code,Country,Home_Phone,Mobile_Phone,Individual_Company,Work_Phone,Work_Extension,Region,Date_Created, Last_Meeting_Date, Referred_By, c.Note, ct.Contact_Type, i.Do_Not_Mail FROM INDIVIDUAL_T i INNER JOIN CONTACT_T c ON c.Contact_ID=i.Individual_ID INNER JOIN INDIVIDUAL_WORK_INFO_T iw ON iw.Individual_ID =i.Individual_ID INNER JOIN CONTACT_TYPE_T ct ON c.Contact_Type=Contact_Type_ID".$filterfrom." WHERE Is_Student = 'No' AND Is_Contact = 'Yes'".$filterwhere." ORDER BY Last_Name, First_Name"; $result = mysql_query($query); $numberOfRows = mysql_numrows($result);?> <html> <head> <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"> <script type="text/javascript" src="http://stackoverflow.com/questions/15796518/myJavaScripts.js"></script> <script type="text/javascript" src="http://stackoverflow.com/questions/15796518/tableSort.js"></script> <link REL=StyleSheet HREF="http://stackoverflow.com/questions/15796518/formatStyles.css" TYPE="text/css" MEDIA=screen,print> <title>Display all contacts...</title> </head> <body> <h3>Display <?php echo $numberOfRows; ?> contacts...</h3> <div style="text-align: left; font-family: Arial;"><small><big><big> <!-- <span style="font-family: Helvetica,Arial,sans-serif;"><a href="http://stackoverflow.com/questions/15796518/reports.php">Back To Reports!</a> </span> --><form action="getCSV.php" method ="post" > <input type="hidden" name="csv_text" id="csv_text"><input type="submit" value="http://stackoverflow.com/questions/15796518/Get CSV File" onclick="getCSVData()"></form><script type="text/javascript" src="http://stackoverflow.com/questions/15796518/javaExport.js"></script><script type="text/javascript">function getCSVData(){ var csv_value=http://stackoverflow.com/questions/15796518/$('#displayAllContacts').table2CSV({delivery:'value'}); $("#csv_text").val(csv_value); }</script><div style="text-align: left; font-family: Arial;"><small><big><!--<div id="Book1_10219" align=center x:publishsource="Excel">--><?php(then it goes on to define the headers and place the results in table cells)\[/code\]I apologize for my ignorance, but i have been staring at the code for several days now and have no idea what else to do
 
Back
Top