如何将表中的查询结果导出到csv?

时间:2013-04-03 19:39:36

标签: php javascript html sql

所以我有几个页面查询我们的数据库,然后将结果显示在网页上的一个大表中。还有一个按钮,允许用户将结果导出到csv文件。这个第一个代码片段运行得非常好。但是其他几个页面不起作用并打开一个没有任何内容的csv文件。上周我得到了简短的工作,但后来又停止了工作。当我开始工作时,它似乎是格式不一致,这解释了为什么它可以在一个页面而不是另一个页面上工作。也正因为如此,我不相信出口它的js存在问题

所以这是工作片段:

</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="myJavaScripts.js"></script>
   <script type="text/javascript" src="tableSort.js"></script>
   <title>Display CEPs Info...</title>
   <link REL=StyleSheet HREF="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="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="Get CSV File" 
        onclick="getCSVData()">
</form>

<script type="text/javascript" src="javaExport.js">

</script>
<script type="text/javascript">
function getCSVData(){
 var csv_value=$('#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)

以下是无效的代码:

 </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="myJavaScripts.js"></script>
    <script type="text/javascript" src="tableSort.js"></script>
   <link REL=StyleSheet HREF="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="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="Get CSV File" 
        onclick="getCSVData()">
</form>

<script type="text/javascript" src="javaExport.js">

</script>
<script type="text/javascript">
function getCSVData(){
 var csv_value=$('#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)

我为自己的无知而道歉,但我已经盯着代码好几天了,不知道还能做什么

0 个答案:

没有答案