通过 PHP 编辑 csv 文件


Editing csv files through PHP

我正在为我的媒体课在网页上做测验。我是编程新手。 我需要学习如何编辑我的数据csv文件以使其更有用。目前,当我查看我的csv文件时,我只得到四列数字(测验的结果)。我需要添加两列,一列包含时间戳,另一列显示填写测验的人员的 IP 地址。

我的PHP的一部分看起来像这样:

    $csv_filehandle = fopen("/var/www/html/data/quiz.csv",'a');
    fputcsv($csv_filehandle,array($summer,$spring,$fall,$winter));
    fclose ($csv_filehandle);
    $visit_id = uniqid('',TRUE);
    $all_my_variables = array(
        'timestamp' => $current_timestamp,
        'user_ip' => $_SERVER['REMOTE_ADDR'],
        'summer' => $summer,
        'spring' => $spring,
        'fall' => $fall,
        'winter' => $winter,
    );
    $json_filehandle = fopen("/var/www/html/data/$visit_id.json",'w');
    fwrite($json_filehandle,json_encode($all_my_variables));
    fclose($json_filehandle);


    $current_hour = date_default_timezone_set("America/Chicago");
    $current_timestamp = date(DATE_RFC822);

在哪里/如何添加这些列,以便它们显示在我的 csv 文件中?帮助将不胜感激!

您正在使用带有 PHP 的平面文件数据库。 您的即时选择:

丰富文本文件的使用:

  • http://www.designdetector.com/archives/04/10/FlatFileDatabaseDemo.php
  • http://www.xentrik.net/php/flatfile.php

升级到最小数据库。
首选工具是SQLite:

  • http://www.switchonthecode.com/tutorials/php-tutorial-creating-and-modifying-sqlite-databases
  • http://fwebde.com/php/sqlite-php/

这会在末尾添加两列,时间戳和 IP。

fputcsv( $csv_filehandle,
        array($summer,
            $spring,
            $fall,
            $winter, 
            time(), 
            $_SERVER['REMOTE_ADDR'] ) 
);