无法生成.csv文件:PHP

时间:2013-01-02 14:00:06

标签: php csv

实际上我不知道如何生成.csv文件。我已经浏览了各种电子笔记,但仍然没有。

我想做什么?

我写了一段代码来回应“ partner_name ”和“链接”,但这些代码需要放入 formDetails.csv 文件。

$result = mysql_query(
    "SELECT form_id, partner_name 
     FROM `ef_kabaadkhana`.`ef_form_master_v1` 
     WHERE active = 1 AND listed = 1;"
);

while ($formDetails = mysql_fetch_assoc($result)) {
    $partner_name       = $formDetails['partner_name'];
    $formId             = $formDetails['form_id'];
    $slugifyPartnername = slugify($partner_name);
    $partner_link       = "http://myforms.com/form/{$formId}/{$slugifyPartnername}";
    echo $partner_name . '=>' . $partner_link, "\n";
}

function slugify($text) {
    $text = preg_replace('~[^\\pL\d]+~u', '-', $text);
    $text = trim($text, '-');
    if (function_exists('iconv'))
        $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
    $text = strtolower($text);
    $text = preg_replace('~[^-\w]+~', '', $text);
    if (empty($text))
        return 'n-a';
    return $text;
}

它的作用是什么?

它只需从ef_form_master_v1表中获取form_id和partner_name。相应地定义并显示$partnerName$partnerlink

但我不想回复$partnerName$partnerlink,而是将这些数组参数放在名为formDetails.csv的CSV文件中。

4 个答案:

答案 0 :(得分:6)

documentation on fputcsv非常明确,但我将在下面提供一个示例:

$handle = fopen('/path/to/formDetails.csv', 'w');

while(<your query loop>) {
    $write = array($partner_name, $partner_link);
    fputcsv($handle, $write);
}

fclose($handle);

答案 1 :(得分:3)

$fp = fopen('formDetails.csv', 'w');
while ($formDetails = mysql_fetch_assoc($rs)) 
{
    $partner_name=$formDetails['partner_name'];
    $formId=$formDetails['form_id'];
    $slugifyPartnername=slugify($partner_name);
    $partner_link="http://myforms.com/form/{$formId}/{$slugifyPartnername}";
    echo $partner_name.'=>'.$partner_link;
    fputcsv($fp, array($partner_name, $partner_link));
}
fclose($fp);

确保formDetails.csv存在于您指定的当前目录或路径中。

答案 2 :(得分:2)

首先你需要创建文件,我建议以SplFileObject

的形式
$csvFile = new SplFileObject('formDetails.csv', 'w+');

您可能还想创建包含列标签的第一行,这可以通过使用包含每列的条目的数组和SplFileObject::fputcsv方法来实现:

$cvsFile->fputcsv(array('Partner', 'Link'));

然后,代替(或旁边用于调试目的):

echo $partner_name . '=>' . $partner_link, "\n";

您将其输出到该文件中:

$csvFile->fputcsv(array($partner_name, $partner_link));

就是这样。上面的观点:

$result = mysql_query(
    "SELECT form_id, partner_name 
     FROM `ef_kabaadkhana`.`ef_form_master_v1` 
     WHERE active = 1 AND listed = 1;"
);

### Create CSV File ###
$csvFile = new SplFileObject('formDetails.csv', 'w+');
$cvsFile->fputcsv(array('Partner', 'Link'));


while ($formDetails = mysql_fetch_assoc($result)) {
    $partner_name       = $formDetails['partner_name'];
    $formId             = $formDetails['form_id'];
    $slugifyPartnername = slugify($partner_name);
    $partner_link       = "http://myforms.com/form/{$formId}/{$slugifyPartnername}";
    echo $partner_name . '=>' . $partner_link, "\n";

    ### Output each row to CSV File ###
    $csvFile->fputcsv(array($partner_name, $partner_link));    
}

function slugify($text) {

    ...

答案 3 :(得分:0)

希望我的示例有所帮助(客户希望csv文件兼容Excel):

$data = $this->_getParam('data'); // getting array of some values
$text = $data[0] . ";";
$text .= $data[1] . ";";
$text .= "\"" . $data[2] . " (N:" . $data[3] . ")" . "\";";
$text .= $data[3];

$ext = '.csv';
$filename = date("Y-m-d") . $ext;
$way = "files/{$filename}";

$fp = fopen($way, "a");
fwrite($fp, $text . PHP_EOL);
fclose($fp);

如果没有这样的csv文件,它将创建一个。