【问题标题】:Why won't this CSV print out correctly? (Magento Categories)为什么这个 CSV 不能正确打印出来? (Magento 类别)
【发布时间】:2023-04-07 22:25:01
【问题描述】:

我有一个脚本,旨在将 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;

以前,我不知道为什么当时和现在打印得很好,只是每一行都在一列中,用逗号分隔。

现在它只是一堆乱七八糟的符号(在 OpenOffice Calc 中)。在打开文件之前出现“过滤器选择”时,我选择 Text CSV,然后选择 UTF-8 编码,然后它会在 OpenOffice Writer (?) 中打开它:

��#ࡱ#�#################;###�� ################## #########����####����########�������������������� ������������������������������������������������������ ������������������������������������������������������ ������������������������������������������������������ ������������������������������������������������������ ������������������������������������������������������ ������������������������������������������������������ ������������������������������������������������������ ������������������������������������������������������ ������������������������������������������������������ ������������������������������������������������������ ������������������������������������������������������ ������������������������������������������������������ ������������������������������������������������������ ������������������������������������������������������ ������������������������������������������������������ ������������������������������������������������������ ������������������������������������������������������ ���������������������������������������������������� �������������������������R#o#o#t#

E#n#t#r#y##################################### ###########������������########################### #########����#################################### ######################################����������� �###################################����######### ################################################# #################������������##################### ##############����############################## ############################################����� �������####################################����

请问我做错了什么?

这毫无意义。将文件更新为:

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 打开。

【问题讨论】:

  • 乍一看,您的代码没有任何问题。它看起来是一些编码问题或 OpenOffice 的问题。如果您在文本编辑器而不是 OpenOffice 中打开文件会发生什么?如果将print_r($data); echo '&lt;br&gt;'; 替换为implode(',' $data) . "\n"; does it look right? And if you replace the print_r` 为var_dump,那么所有内容都是(str) 类型的吗?您在服务器上设置了什么编码?
  • 不确定编码。如果我在记事本中打开它,它是直接空白的。 var_dump 确实显示字符串。
  • 我添加了 header('Content-Type: text/html; charset=utf-8');到顶部,它仍然在记事本中打开空白。我可以在这里做什么?

标签: php csv magento


【解决方案1】:

哦,哇,这个函数没有返回任何东西,显然它没有添加到 $rows 中,因为 $rows 是在它之外初始化的。我是个白痴。

【讨论】:

  • 所以我没有看到这个明显的错误 :-) 这些是最难的!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-13
  • 2013-04-19
  • 2023-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-23
相关资源
最近更新 更多