How to write only latest values with DSVWriter?

Plugins skapade av Switch Kings användare | Plugins created by Switch King users
Post Reply
pekkak
2 stars
2 stars
Posts: 44
Joined: Sat 10 Nov 2012, 07:31

How to write only latest values with DSVWriter?

Post by pekkak » Sun 06 Jan 2013, 18:25

I need a text file of e.g. 100 latest temperature values to draw a graph. If I use DSVWriter plugin with setting "append=true", the file keeps growing forever. Is there anyway to make it delete the first line when it adds a new line in the end when a specified number of rows have been reached?

Or can I somehow (preferably with PHP) query this data from Switch King DB?

pekkak
2 stars
2 stars
Posts: 44
Joined: Sat 10 Nov 2012, 07:31

Re: How to write only latest values with DSVWriter?

Post by pekkak » Sun 06 Jan 2013, 20:39

Before a more sofisticated solution I will clean up the beginning of the file leaving 40 rows in the end and create headers with PHP like this:

$csvfile = "ulko.csv";
$keep = 40;
$n = count(file($csvfile));
if ($n > $keep) {
$lines = file($csvfile);
$first_line = $lines[0];
$lines = array_slice($lines, $n - $keep);
$lines = array_merge(array("Temperature;Time\n"), $lines);
$file = fopen($csvfile, 'w');
fwrite($file, implode('', $lines));
fclose($file);
}

Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests