我在PHP中创建CSV文件,但也显示CSV文件结果和所有头文件中的所有内容

时间:2013-05-22 11:17:36

标签: php javascript jquery ajax

在这段代码中,我使用两个按钮生成按钮,在同一页面上显示所有结果此按钮正常工作,当我点击if($_POST['btnDownload'] == 'Download')生成文件时生成的csv文件显示结果但是显示所有像这样的内容: - (

<html>                  
    <head>                  
    <!--<meta http-equiv=""content-type"" content=""text/html; charset=UTF-8"">-->                  
    <meta http-equiv=""Content-Type"" content=""text/html; charset=UTF-8"" />                   
    <title>Hotel menu management </title>               
    <link rel=""stylesheet"" href=""http://192.168.1.50/hotelmenu//admin/css/style.css"" type=""text/css"">                 
    <script type=""text/javascript"" src=""dtree.js""></script>                 
    <script type=""text/javascript"" src=""main.js""></script>                  
    <script type=""text/javascript"" src=""prototype.js""></script>                 
    <script type=""text/javascript"" src=""addmore.js""></script>

显示此内容的原因是什么,我的代码正常工作并显示完整的结果,以便我可以做什么来面对这个问题..........

if($_POST['btnGenerate'] == 'Generate')
{
    $sql="select order_master.o_id as o_id,cust_id,tableid,time(order_date) as time,amount,order_master.note as onote ,order_details.note as note,count(order_details.o_id) as total_count
    from order_master
    inner join order_details on order_details.o_id = order_master.o_id
    where order_master.hotel_id = '$hid' and order_master.order_date <= CURRENT_TIMESTAMP() 
    and   order_master.order_date >=now() - INTERVAL 1 DAY 
    and status = 'pending'
    group by order_master.o_id ";
    /*echo $sql;
    exit;*/

    $res=mysql_query($sql);
    $sql1 = " select count(status) as count from bill_generation_request where hotel_id = '$hid' and status ='yes'";

    $result = mysql_query($sql1);

            while ($row = mysql_fetch_array($result))
        {
        $count = $row['count'];
        }

     echo '<table align="center" cellspacing="0" cellpadding="5" width="0%" border="1"> <tr>        
            <td align="left"><b>Bill Request Count</b></td>';   
     echo '<td align="left">' .$count . '</td> ';
     echo '</tr>';
     echo '</table>';
    echo '<table align="center" cellspacing="0" cellpadding="5" width="75%" border="1">
     <tr>
     <td align="left"><b>Order Number</b></td>
     <td align="left"><b>Table Number</b></td>
     <td align="left"><b>Item Count</b></td>
     <td align="left"><b>Order Time</b></td>
     <td align="left"><b>Total Amount</b></td>
     <td align="left"><b>Order Note</b></td>
     <td align="left"><b>Note</b></td>
     </tr> ';
    $i=0;
    while ($row = mysql_fetch_array($res))
        {   
            $id = $row['o_id'];
         echo '<tr bgcolor="' . $bg . '">
             <td align="left"><a href="admin_order_update.php?id='.$id.'">' .$row['o_id'] .'</a></td>
             <td align="left">' . $row['tableid'] . '</td>
             <td align="left">' . $row['total_count'] . '</td>
             <td align="left">' .$row['time'] . '</td>
             <td align="left">' .$row['amount'] . '</td>
             <td align="left">' .$row['onote'] . '</td>
             <td align="left">' .$row['note'] . '</td>
             </tr>';

        $i++;
        }

     echo '</table>';
}
if($_POST['btnDownload'] == 'Download')
{
    $filename = "sales_report" . date('Ymd') . ".csv";
    header('Content-type: application/CSV');
    header("Content-Disposition: attachment; filename=\"$filename\"");
    $file_content = "Sr.No.,cust_id,tableid,time,amount,onote,total_count";
    //echo $file_content;
    $file_content .= "\r\n";
    $download = 

     $query = "select order_master.o_id as o_id,cust_id,tableid,time(order_date) as time,amount,order_master.note as onote ,order_details.note,count(order_details.o_id) as total_count
    from order_master
    inner join order_details on order_details.o_id = order_master.o_id
    where order_master.hotel_id = '$hid' and order_master.order_date <= CURRENT_TIMESTAMP() 
    and   order_master.order_date >=now() - INTERVAL 1 DAY 
    and status = 'pending'
    group by order_master.o_id ";

     $rs = mysql_query($query);
     $num_rows = mysql_num_rows($rs);
     $k = 1;
     for( $i= 0; $i<$num_rows; $i++){
         $number = mysql_result($rs,$i,'cust_id');
        $client_city = mysql_result($rs,$i,'tableid');
         $time = mysql_result($rs,$i,'time');
        $amount = mysql_result($rs,$i,'amount');
        $onote = mysql_result($rs,$i,'onote');
        $total_count = mysql_result($rs,$i,'total_count');

     $file_content .=$k .','.$number.','.$client_city.','.$time.','.$amount.','.$onote.','.$total_count."\r\n";
     $k++;
      }

     echo $file_content;
}

2 个答案:

答案 0 :(得分:0)

您缺少csv的格式。它包括带引号的值。

例如:

abc  = "abc"

"abc" = "\"abc\""
  1. 使用双引号(“)代替单引号(')
  2. 用双引号(“abc”)
  3. 包装所有条目
  4. 在需要时转义(“\”abc \“”)

答案 1 :(得分:0)

如果要强制下载生成的csv而不是在浏览器窗口中将其显示为内容 - 在回显生成的内容(或一般的任何输出)之前使用这组标题:

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-Transfer-Encoding: binary");
header("Content-Disposition: attachment; filename=name_of_file.csv;");