T
T
TechNOIR2018-02-26 16:06:23
PHP
TechNOIR, 2018-02-26 16:06:23

PHP. How to output variables line by line in CSV?

Good afternoon.
Tell me please, comrades. How to output variables line by line in CSV?
I have 2 variables in the chat bot $q (what we enter), $r (what the bot answers)
I want to log in a csv table in two columns $q and $r
How to write line by line to csv? So that the record is not overwritten, but continues below with a new line

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Arushanov, 2018-02-26
@daruwanov

How are you writing to the file? Most likely the write modifier is set to overwrite and to "overwrite"

A
Alexander, 2018-02-26
@AK-VoronM

$file = '/path/to/file/file_name';
$string = 'Тут ваша переменная';
if (file_exist($file)) {
    file_put_contents($file, $string, FILE_APPEND | LOCK_EX); //Добавит строку в файл
} else {
    file_put_contents($file, $string);//Создаст файл и положит в него строку
}

Somehow it is possible.

A
artem78, 2018-02-26
@artem78

Open the file with write access ("a").

$fh = fopen('log.csv', 'a');

$q = 'Как тебя зовут?';
$r = 'Петя';
fputcsv($fh, array($q, $r));

fclose($fh);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question