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 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据