我有一个脚本,用于输出Magento类别'数据到CSV文件:
<?php
require_once 'app/Mage.php';
Mage::app();
$categoryId = 228;
$category = Mage::getModel('catalog/category')->load($categoryId);
$outputFile = "var/export/categories-and-ids.csv";
$write = fopen($outputFile, 'w');
echo '<pre>';
function writeoutcategory($parent)
{
$_categories = $parent->getChildrenCategories();
foreach($_categories as $cat)
{
$data = [/*
'"'.trim($cat->getName()).'"',
'"'.$cat->getId().'"',
'"'.trim($cat->getParentCategory()->getName()).'"',
'"'.$cat->getParentCategory()->getId().'"',*/
trim($cat->getName()),
$cat->getId(),
trim($cat->getParentCategory()->getName()),
$cat->getParentCategory()->getId(),
];
print_r($data); echo '<br>';
fputcsv($write, $data);
if(count($cat->getChildrenCategories())) writeoutcategory($cat);
}
}
writeoutcategory($category);
echo '</pre>';
fclose($write);
echo "File written at ".$outputFile;
&#13;
之前和我都不知道为什么然后现在它没有打印好,只是每一行都在一行中用逗号分隔。
现在它出现了一堆乱码(在OpenOffice Calc中)。当&#34;过滤器选择&#34;在打开文件之前出现,我选择文本CSV,然后选择UTF-8编码,然后在OpenOffice Writer(?)中打开它:
��#ࡱ#�################; ###��################### ######################R·○△○#吨#
电子#N#T#R·ÿ###################################### ###################################### ############################################## ####################################### ############################################# ################################################## ###################################### ############################################## ############################################# ####################################
我做错了什么,拜托?
这没有任何意义。将文件更新为:
header('Content-Type: text/html; charset=utf-8');
require_once 'app/Mage.php';
Mage::app();
$categoryId = 228; // PTL > Tablecloths
$category = Mage::getModel('catalog/category')->load($categoryId);
echo '<pre>';
$rows = [];
function collectCategories($parent)
{
$_categories = $parent->getChildrenCategories();
foreach($_categories as $cat)
{
$data = [
trim($cat->getName()),
$cat->getId(),
trim($cat->getParentCategory()->getName()),
$cat->getParentCategory()->getId(),
];
var_dump($data);
$rows[] = $data;
if(count($cat->getChildrenCategories())) collectCategories($cat);
}
}
collectCategories($category);
echo '</pre>';
$outputFile = "var/export/categories-and-ids.csv";
$write = fopen($outputFile, 'w');
foreach($rows as $row) fputcsv($write, $row);
fclose($write);
echo "File written at ".$outputFile;
它仍然在OO Calc中打开上面的内容并在记事本中显示为空白。什么可能是错的?
更新:现在我只是简单地附上了这个文件:
$list = array (
array('aaa', 'bbb', 'ccc', 'dddd'),
array('123', '456', '789'),
array('"aaa"', '"bbb"')
);
$fp = fopen('var/export/file.csv', 'w');
foreach ($list as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
哪个输出(Atom):
aaa,bbb,ccc,dddd
123,456,789
"""aaa""","""bbb"""
这在OO Calc中:
aaa,bbb,ccc,dddd
123456789
"aaa","""bbb"""
(一栏)
我的categories-and-ids.csv仍为空白,并且不会通过OO以CSV格式打开。
答案 0 :(得分:0)
OH哇这个函数没有返回任何东西显然它没有添加到$行,因为$ rows被初始化在它之外。我是个白痴。