dongmingxiang0312 2013-11-20 09:01
浏览 58
已采纳

PHP到CSV无法正确输出字段

I am trying to output my PHP code to a downloadable CSV file and so far i am able to do this however at the moment, the output does not place the field data into separate columns. Instead it puts everything into one column and in addition to that, it displays the postcodes in its neighbouring columns. Does anyone know why this is?

<?php
header("Content-type: text/csv");  
header("Cache-Control: no-store, no-cache");  
header('Content-Disposition: attachment; filename="filename.csv"');  

$outstream = fopen("php://output",'w');  

if ($db_found) {

// Select all fields from bodyshops_master_network table
$SQL = "SELECT * FROM bodyshops_master_network";
$result = mysql_query($SQL);

while ( $db_field = mysql_fetch_assoc($result) ) {

    // Splits the arrays
    $row = explode(",", $db_field['postcodes_covered']);

        $dealer_code = $db_field['dealer_code'];
        $dealer_name = $db_field['dealer_name'];
        $bodyshop_id = $db_field['bodyshop_id'];
        $bodyshop_name = $db_field['bodyshop_name'];
        $address1 = $db_field['address1'];
        $address2 = $db_field['address2'];
        $address3 = $db_field['address3'];
        $address4= $db_field['address4'];
        $address5 = $db_field['address5'];
        $postcode = $db_field['postcode'];
        $bo_tel = $db_field['BO_tel'];
        $bo_email = $db_field['BO_email'];
        $bc_name = $db_field['BC_name'];
        $equity_contract = $db_field['equity_contract'];

    echo "<pre>";
    foreach ($row as $value) {

        echo $dealer_code . " ";
        echo $dealer_name . " ";
        echo $bodyshop_id . " ";
        echo $bodyshop_name . " ";
        echo $address1 . " ";
        echo $address2 . " ";
        echo $address3 . " ";
        echo $address4 . " ";
        echo $address5 . " ";
        echo $postcode . " ";
        echo $bo_tel . " ";
        echo $bo_email . " ";
        echo $bc_name . " ";
        echo $equity_contract . " ";
        echo $value. "<BR>";

       fputcsv($outstream, $row, ',', '"');  
    }
        echo "</pre>";

}

mysql_close($db_handle);

} else {

print "Database NOT Found ";
mysql_close($db_handle);
fclose($outstream);  
}

?>
  • 写回答

1条回答 默认 最新

  • doumiao0498 2013-11-20 09:18
    关注

    OK, you're doing something really weird here. You're outputting content in two ways.

    First, you output content in the normal way using echo. Fair enough!

    Second, you output content using fputcsv where the file handler leads to php://output. Also fair enough!

    But you shouldn't do both and expect it to work seamlessly. You want a CSV file, so the best way is to output all your data using fputcsv. At the moment, some of your data is encapsulated as a CSV file, and some of it isn't. This leads to output that you don't really want.

    I think you want something like this:

    foreach ($row as $value) {
        $output = array(
           $dealer_code,
           $dealer_name,
           $bodyshop_id,
           $bodyshop_name,
           $address1,
           $address2,
           $address3,
           $address4,
           $address5,
           $postcode,
           $bo_tel,
           $bo_email,
           $bc_name.
           $equity_contract,
           $value
       );
    
       fputcsv($outstream, $output, ',', '"');  
    }
    

    fputcsv expects the data to be an array, so that's what we give it. The separate array fields will be separate columns in the CSV output. We don't echo any data: we store it in an array and then supply it to fputcsv.

    The second mistake you made was to send $row rather than $value to fputcsv. This meant that you were sending all the postcodes as received from the database row.

    Finally, you don't need <pre>.

    On a separate note, you really should have a better database structure. If you had a table of postcodes and a separate table of bodyshops and a foreign key linking them, the code to retrieve data would be a lot simpler, as would searching and updating it in future.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。