douyuliu9527 2016-03-25 00:15
浏览 65
已采纳

PHP更新CSV - fput()

I want to open a csv file with following entries:

Firma,Anrede,Titel,Vorname,Nachname,Strasse,PLZ,Telefon,Fax,Email,Nachricht,CopyFlag,Importiert,Importdate,Importtime
Testfirma1,Herr,Dr.,Vorname1,Name1,Strasse1,11111,12345567,123456,1@keine.de,nachricht1,1,,24.03.2016,22:00:00
Testfirma2,Herr,Dr.,Vorname2,Name2,Strasse2,22222,22345567,223456,2@keine.de,nachricht2,1,,25.03.2016,22:00:00
Testfirma3,Herr,Dr.,Vorname3,Name3,Strasse3,33333,32345567,323456,3@keine.de,nachricht3,1,,25.03.2016,22:00:00
Testfirma4,Herr,Dr.,Vorname4,Name4,Strasse4,44444,42345567,423456,4@keine.de,nachricht4,1,,25.03.2016,22:00:00

Than I want to check if there's nothing in Column 12 (Importiert), and check if the importtime is < than the systemtime. If both are true I want to send this data to curl doing some post. When this is done, I want to write a timestamp in column 12. I tried it with this example:

$fp = fopen("Testformular.csv", "r+"); 
$line = 0;
while ( $row = fgetcsv($fp) ) {
    if($line === 0){
        $line++;
        continue;};
   $actual = date("d.m.Y - H:i:s", time());
   $csvtime=  date("d.m.Y - H:i:s", strtotime($row[13]. $row[14]));
   if (empty($row[12])){
        if ($actual > $csvtime){
            $output = "";
            $output .= 'tx_btsimplecontact_pi1[name]=' . urlencode("");
            $output .= '&tx_btsimplecontact_pi1[firma]=' . urlencode($row[0]);
            $output .= '&tx_btsimplecontact_pi1[sex]=' . urlencode($row[1]);
            $output .= '&tx_btsimplecontact_pi1[titel]=' . urlencode($row[2]);
            $output .= '&tx_btsimplecontact_pi1[vorname]=' . urlencode($row[3]);
            $output .= '&tx_btsimplecontact_pi1[nachname]=' . urlencode($row[4]);
            $output .= '&tx_btsimplecontact_pi1[strasse]=' . urlencode($row[5]);
            $output .= '&tx_btsimplecontact_pi1[plz_ort]=' . urlencode($row[6]);
            $output .= '&tx_btsimplecontact_pi1[telefon]=' . urlencode($row[7]);
            $output .= '&tx_btsimplecontact_pi1[fax]=' . urlencode($row[8]);
            $output .= '&tx_btsimplecontact_pi1[email]=' . urlencode($row[9]);
            $output .= '&tx_btsimplecontact_pi1[nachricht]=' . urlencode($row[10]);
            $output .= '&tx_btsimplecontact_pi1[copy]=' . urlencode($row[11]);
            $output .= '&submit=Abschicken';
            print_r ($row);
            print ($output);        
            $row[12]= $actual;      
            print_r ($row);
            fputcsv($fp,$row);  
            };
        };
curl ('http://www.post.html', "$output");
};

The output in CSV is following:

Firma,Anrede,Titel,Vorname,Nachname,Strasse,PLZ,Telefon,Fax,Email,Nachricht,CopyFlag,Importiert,Importdate,Importtime
Testfirma1,Herr,Dr.,Vorname1,Name1,Strasse1,11111,12345567,123456,1@keine.de,nachricht1,1,,24.03.2016,22:00:00
Testfirma1,Herr,Dr.,Vorname1,Name1,Strasse1,11111,12345567,123456,1@keine.de,nachricht1,1,"25.03.2016 - 01:10:15",24.03.2016,22:00:00
name3,Name3,Strasse3,33333,32345567,323456,3@keine.de,nachricht3,1,,25.03.2016,22:00:00
name3,Name3,Strasse3,33333,32345567,323456,3@keine.de,nachricht3,1,,25.03.2016,22:00:00,"25.03.2016 - 01:10:16"
  • 写回答

1条回答 默认 最新

  • douchaqi3369 2016-03-25 00:45
    关注

    CSV is a flat file, and there are two ways to update a flat file:

    1. Append to the end.
    2. Rewrite the entire thing.

    You want #2, but you've done #1. #2 is also fairly inefficient, so in cases like yours most people tend to switch to a database.

    If you need something lightweight that doesn't require setting up a server there's always SQLite.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?