Click here to Skip to main content
15,033,303 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have written the below code to generate a report in excel but it is only showing the headers on webpage,not even generating excel file.please help me in this regards.

PHP
<?php
error_reporting(0);
  include("database.php");


    $fromdate= $_GET['fromdate'];
    $todate = $_GET['todate'];
    echo $fromdate;
//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;
 $out .= "\n\n";
 $result1 = mysql_query("SELECT * from bookings where (date)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;
             $csv_output .= "\n";
             $out .= $csv_output ."\n\n";

$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: attachment; filename=".$filename.".csv");

//Print the contents of out to the generated file.
echo "\n                                                               "." Report of FirstLook Preview";

print $out;
exit;
?>


NEW UPDATE:when is commented error_reporting(0); I get these errors:


Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home/public_html/mysite.com/admin/loggedin/bet_date_excel.php on line 18

Warning: Cannot modify header information - headers already sent by (output started at /home/vrwebqbj/public_html/mysite.com/admin/loggedin/bet_date_excel.php:8) in /home/public_html/mysite.com/admin/loggedin/bet_date_excel.php on line 37

Warning: Cannot modify header information - headers already sent by (output started at /home/public_html/mysite.com/admin/loggedin/bet_date_excel.php:8) in /home/public_html/mysite.com/admin/loggedin/bet_date_excel.php on line 39
Report of FirstLook Preview Id,Date,Time,Movie,Name,Email,Phone,Approved , , , , , , ,
Posted
Updated 1-Feb-15 19:46pm
v3
Comments
Arun-23 2-Feb-15 4:52am
   
date is a predefined type in mysql don't use it as a field name try different name for date

1 solution

Try like this

PHP
<?php
$filename = "testing-exports.csv";

header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=$filename");
header("Pragma: no-cache");
header("Expires: 0");

$sql = "SELECT * FROM tbl_products1 WHERE 1";

try {
    $stmt = $DB->prepare($sql);
    $stmt->execute();
    $results = $stmt->fetchAll();
} catch (Exception $ex) {
    printErrorMessage($ex->getMessage());
}

$content = array();
$title = array("Name", "Quantity", "Model", "Price", "Weight", "Status");
foreach ($results as $rs) {
    $row = array();

    $row[] = stripslashes($rs["products_name"]);
    $row[] = stripslashes($rs["products_quantity"]);
    $row[] = stripslashes($rs["products_model"]);
    $row[] = stripslashes($rs["products_price"]);
    $row[] = stripslashes($rs["products_weight"]);
    $row[] = ($rs["products_status"] == "A") ? "Active" : "Inactive";

    $content[] = $row;

}

$output = fopen('php://output', 'w');
fputcsv($output, $title);
foreach ($content as $con) {
    fputcsv($output, $con);
}

?>
   

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