使用PHP创建.csv文件

时间:2014-03-21 15:43:01

标签: php csv opencart

我正在尝试使用以下代码创建.csv文件:

public function export_modis_categories_xml(){  
header('Content-Type: application/excel');
header('Content-Disposition: attachment; filename="category_export.csv"');    
$category_rs = $this->db->query('
  SELECT        
    '.DB_PREFIX.'category.category_id, 
    '.DB_PREFIX.'category.parent_id, 
    '.DB_PREFIX.'category_description.name, 
    '.DB_PREFIX.'category_description.description 
  FROM          
    '.DB_PREFIX.'category
  INNER JOIN    
    '.DB_PREFIX.'category_description
  ON    
    '.DB_PREFIX.'category.category_id = '.DB_PREFIX.'category_description.category_id
');
error_log($category_rs);
$category_rs = $category_rs->rows;
$category_export_array = array();
$i = 0;
foreach($category_rs as $cat){
  $category_id  = ($category_rs[$i]['category_id']  =! '' ? $category_rs[$i]['category_id'] : '');
  $parent_id    = ($category_rs[$i]['parent_id']    =! '' ? $category_rs[$i]['parent_id']   : '');
  $name         = ($category_rs[$i]['name']         =! '' ? $category_rs[$i]['name']        : '');
  $description  = ($category_rs[$i]['description']  =! '' ? $category_rs[$i]['description'] : '');

  $category_export_array[$i] = $category_id.';'.$parent_id.';'.$name.';'.$description;      
  $i++;  
}

$fp = fopen('php://output', 'w');
$header_array = array('headers'=>'categorie_id;parent_id;name;description');
fputcsv($fp, $header_array);
fputcsv($fp, $category_export_array);
fclose($fp);  
} 

这适用于一个网站,因为它是一个OpenCart插件,但不在另一个网站上。

奇怪的是,我没有得到任何错误。不在错误日志或Web视图中。它没有给我文件,网站也没有空白......

1 个答案:

答案 0 :(得分:0)

我发现了这个错误,它从来没有在我的代码中......

调用该函数的表单未发送正确的POST名称,因此该函数未被触发。

很抱歉因为我的愚蠢而打扰每个人..