user13405941
user13405941

Reputation:

PHP set specific line in CSV

I am trying to set a specific line in a CSV file. I tried changing $line, but that (obviously) only changes the variable and not the line. I also tried getting the line number but it´s still a mystery how I can write to this specific line only for me.

My Code:

$file = fopen(WP_CONTENT_DIR."/plugins/coinStats/coin_database.csv","w");

  //Search file; If the number is there, get data. If not, create
  while (($line = fgetcsv($file)) !== FALSE) {
    if (in_array($CoinNumber, $line)) {
      $find = $line;
      $foundResult = true;
      //Change line here?
    }
  }

Upvotes: 2

Views: 175

Answers (1)

Honk der Hase
Honk der Hase

Reputation: 2488

Changing data in the (same) CSV is not as easy as you'd think.

I would suggest to use a second file, eg. read from the first file, write to the second file. When done, delete the first file and rename the second file.

Upvotes: 1

Related Questions