Click here to Skip to main content
15,891,833 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I need to generate a CSV file from php and mysql for data between the two dates.
here is my code:

PHP
From date: <input type="text" id="fromdate" value="" onChange="showuser()">   To date: <input type="text" id="todate" value="" onChange="showuser()">  <input type="button" id="btn_bet_date" value="Export in excel">


Ajax:


JavaScript
<script type="text/javascript">
     $(function() {
                    $("#btn_bet_date").click(function() {
                        var fromdate = $("#fromdate").val();
    					 var todate = $("#todate").val();
                        $.post('bet_date_excel.php', { fromdate: fromdate,todate:todate });
                        return false;
                    });
                });
    </script>


bet_date_excel.php:

PHP
<?php
    error_reporting(0);
      include("database.php");
     
    
    	$fromdate= $_GET['fromdate'];
    	$todate = $_GET['todate'];
    //First we'll generate an output variable called out. It'll have all of our text for the CSV file.
    $out = '';
    
     $csv_hdr .= "\n\n\n Id,Date,Time,Movie,Name,Email,Phone,Approved";
     
     $out .= $csv_hdr;
     
     $result1 = mysql_query("SELECT * from bookings where date between '$fromdate' and '$todate'");
     
     while($row = mysql_fetch_array($result1))
    			{
    				 $id= $row["id"];
    				 $date= $row["date"];
    				 $time= $row["time"];
    				 $movie= $row["movie"];
    			     $name= $row["name"];
    				 $email= $row["email"];
    				 $phone= $row["phone"];
    				 $approved= $row["approved"];
    			}
    			 $csv_output .= $id .", ".$date.", ".$time.", ".$movie.", ".$name.", ".$email.", ".$phone.", ".$approved;
    			 
    $filename = "FirstLook-$fromdate-$todate";
    
    //Generate the CSV file header
    
    header("Content-type: application/vnd.ms-excel");
    header("Content-disposition: csv" . date("Y-m-d") . ".csv");
    header("Content-disposition: filename=".$filename.".csv");
    
    //Print the contents of out to the generated file.
    echo "\n                                                               "." Report of FirstLook Preview"; 
    
    print $out;

?>


I have used chrome's webtool also in which in network tab this php file is getting called is showing.

anyone have idea about it?
Posted
Comments
Kornfeld Eliyahu Peter 21-Dec-14 5:08am    
You have $out and $csv_output and you use them inconsistently...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900