0

在此代码中,我使用两个按钮一个用于生成按钮以在同一页面上显示所有结果此按钮正常工作,当我单击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;
}
4

2 回答 2

0

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

例如:

abc  = "abc"

"abc" = "\"abc\""
  1. 使用双引号 (") 而不是单引号 (')
  2. 用双引号括起所有条目(“abc”)
  3. 在需要的地方转义 ("\"abc\"")
于 2013-05-22T11:24:31.110 回答
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;");
于 2013-05-22T11:31:30.173 回答