douqiao5440 2018-08-13 06:58
浏览 74

如何使用PHP从csv文件中编写和获取正确的值?

Previously I had two tables. I have a php script to copy the contents of one table one to table 2, to identify the ids which are copied I made a csv file to check for the last row inserted id like below

 "Date",lastid(int)

Previously I was checking the last id using the index of csv by using fopen as 0 and 1.

But now I have another table 3 to copy to table 2 and the process is same. but When i write for the new table it gets overwritten so I cant identifiy for the previous table. If i append it the file will get extended. Is there any way to put the table 1 contents in the first line and table two last id in the second line as

  "Date",lastid(int), // for t1 copied to t2
  "Date",lastid(int)  // for t3 copied to t2

and taking 0, 1 for table 1 and 2,3 for table 2. Even when table 2 is done first.

public function writeNumberOfRowsCopiedForTable2FromTable1($row_write)
{
    $file = fopen('rowsCopied.csv', 'w+');
    fputcsv($file, $row_write);
    fclose($file);

}

calling it by

writeNumberOfRowsCopiedForTable2FromTable1([$this->global_date, $last_row_copied]);

getting values for inserted values in t2 from t1

 public function getLastRowCopied()
{
    $end_row_id = 0;
    if (file_exists('rowsCopied.csv')) {
        $handle       = fopen('rowsCopied.csv', 'r');
        $data         = fgetcsv($handle);
        $end_row_id += intval($data[1]);
        $this->global_date = $data[0];
        fclose($handle);
    }
    return $end_row_id;

}

Hope I explained the question. Someone knows the answer?

  • 写回答

1条回答 默认 最新

  • doumeba0486 2018-08-13 07:54
    关注

    I've written a couple of methods which should do what your after. You will need to integrate them with how you currently use them, but this is just so I can test them for myself.

    Each method takes a $rowNumber parameter, this is the row in the stored file that you want to set/get the value for. In the code it adjusts it, so row 1 is actually element 0 of the array. I've used file() to read the whole file in and then get/set the row your interested in. In the writeNumberOfRowsCopied method it then uses file_put_contents() to write the whole file out in one go (imploding the lines of the file).

    function writeNumberOfRowsCopied($rowNumber, $row_write)
    {
        $fileName = 'rowsCopied.csv';
        $file = file($fileName, FILE_IGNORE_NEW_LINES);
    
        $file[$rowNumber-1] = implode(",", $row_write);
        file_put_contents($fileName, implode(PHP_EOL, $file));
    }
    
    function getLastRowCopied($rowNumber)
    {
        $fileName = 'rowsCopied.csv';
        $file = file($fileName, FILE_IGNORE_NEW_LINES);
    
        $row = explode(",", $file[$rowNumber-1]);
        return (int)$row[1];
    }
    writeNumberOfRowsCopied(2, ["aa", 22121]);
    print_r(getLastRowCopied(2));
    
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?