Jump to content

Read from a file line by line


flobeats

Recommended Posts

Hy, I have a question..I want to do a demo page to simulate the read of the temperature from a room. I want to do that read from a file.txt. In this file i write data manualy:

18 celsius or just 18

19

20

19

21

...

I want to show this data in my page. php row by row in 30 seconds. First 18, after 30 seconds 19, after 30 sec 20...Enybody have ideeas how cold i do that?

Thanks.

 

 

Link to comment
https://forums.phpfreaks.com/topic/53478-read-from-a-file-line-by-line/
Share on other sites

Javascript isn't my thing, but I can show you how to get a JS array with the values in:

 

<?php

$lines = file("file.txt");

echo "<script type=\"text/javascript\">\nvar temperatures = new Array()\n";

foreach($lines as $number => $temp) {
echo "temperatures[$number] = $temp\n";
}

echo "</script>";

?>

Check out combining 'prototype' (a javascript library) using the AJAX.Request or AJAX.Updater function with your PHP file (if you actually need to use PHP in the first place). Here's a good reference: http://www.sergiopereira.com/articles/prototype.js.html. If you are just reading it from a text file however, you won't need PHP as javascript can access text files directly (i think).

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.