将客户数据拉入csv文件的所有列

时间:2016-02-04 00:15:52

标签: php csv

这是我当前的代码,它将所有数据拉入单个列。我会喜欢的 将数据拉入自己的列。谢谢你的帮助

<?php
include('codelibrary/inc/variables.php');
include("session.php");
$obj= new database_class();

$query = $obj->getAnyTableAllData($obj->getTable("var_customer")," and email != '' order by email ");
$filename = 'customer_email.csv';
$fp = fopen($filename, 'w');

foreach ($query as $k=>$v) {
    $_csv_data= $v["email"]. "\n";
    fwrite( $fp, $_csv_data );
}


foreach ($query as $k=>$v) {
    $_csv_data= $v["first_name"]. "\n";
    fwrite( $fp, $_csv_data );
}


foreach ($query as $k=>$v) {
    $_csv_data= $v["business"]. "\n";
    fwrite( $fp, $_csv_data );
}


foreach ($query as $k=>$v) {
    $_csv_data= $v["phone_no"]. "\n";
    fwrite( $fp, $_csv_data );
}


foreach ($query as $k=>$v) {
    $_csv_data= $v["shipping_address"]. "\n";
    fwrite( $fp, $_csv_data );
}

fclose($fp);

if(is_file($filename))
{
    $size=filesize("$filename");
    header("Content-type: application/csv");
    header("Content-Length: $size");
    header("Content-Disposition: attachment; filename=$filename");
    header("Pragma: no-cache");
    header("Expires: 0");
    readfile($filename);
    exit;
 }
else
{
    echo "Invalid file!";
}
?>

这是我在这里的第一篇文章,我发现这里的所有信息都非常有用。谢谢你所做的一切。

编辑: 以下是语法错误

的当前代码
<?php
include('codelibrary/inc/variables.php');
include("session.php");
$obj= new database_class();

$query = $obj->getAnyTableAllData($obj->getTable("var_customer")," and email != '' order by email ");
$filename = 'customer_email.csv';
$fp = fopen($filename, 'w');


# let's get keys into an order that you like
$fields = array('email','first_name','business','phone_no','shipping_address');

foreach ($query as $k=>$v) {
// put the fields for this row into an array
foreach ($fields as $field) { 
    $data[] = $v[$field];
}
fputcsv($fp, $data);
unset($data);
}

 fclose($fp);

if(is_file($filename))
{
$size=filesize("$filename");
header("Content-type: application/csv");
header("Content-Length: $size");
header("Content-Disposition: attachment; filename=$filename");
header("Pragma: no-cache");
header("Expires: 0");
readfile($filename);

 exit;
 }
 else
  {
        echo "Invalid file!";
         }
?>

3 个答案:

答案 0 :(得分:0)

对于CSV而不是fwrite,您可以使用fputcsv,只需传递一列列即可插入行中。

$array = array('column one contents', 'column two contents'); fputcsv($fp, $array);

答案 1 :(得分:0)

我建议调查fputcsv。

php manual

答案 2 :(得分:0)

看起来您的代码循环遍历所有数据,打印出email。然后再次循环并打印出first_name ...我还没有测试过,但是这使用fputcsv来编写文件 - 我只更改了代码的那些部分。

<?php
include('codelibrary/inc/variables.php');
include("session.php");
$obj= new database_class();

    $query = $obj->getAnyTableAllData($obj->getTable("var_customer")," and email != '' order by email ");
$filename = 'customer_email.csv';
$fp = fopen($filename, 'w');


# let's get keys into an order that you like
$fields = array('email','first_name','business','phone_no','shipping_address');

foreach ($query as $k=>$v) {
    // put the fields for this row into an array
    foreach ($fields as $field) { 
        $data[] = $v[$field];
    }
    $lines[] = $data; // add the row of data to $lines
    unset($data);
}

fputcsv($fp, $lines);

fclose($fp);

if(is_file($filename))
{
    $size=filesize("$filename");
    header("Content-type: application/csv");
    header("Content-Length: $size");
    header("Content-Disposition: attachment; filename=$filename");
    header("Pragma: no-cache");
    header("Expires: 0");
    readfile($filename);

     exit;
     }
 else
      {
            echo "Invalid file!";
             }
?>