3

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

我想做的事?

我已经编写了一段代码来回显“ partner_name ”和“ link ”,但这些需要放入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

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

4

4 回答 4

6

文档fputcsv很清楚,但是我将在下面提供一个示例:

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

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

fclose($handle);
于 2013-01-02T14:06:11.203 回答
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 存在于您指定的当前目录或路径中。

于 2013-01-02T14:07:17.290 回答
2

首先,您需要创建文件,我建议以 a 的形式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) {

    ...
于 2013-01-02T14:20:33.657 回答
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 文件,它将创建一个。

于 2013-01-02T14:10:44.313 回答