-1

We have this telemetry device that update data by sending csv file to our server. The data will be automatically updated every 15 minutes inside csv file. A new csv file will be uploaded everyday. The file will be stored in one folder every month, means that a new folder will be automatically created by the device. (for example the file path: ..\database\AWLR\2018\10)

CSV file name is a date format (for example: 20181002.CSV). Sometimes the data will not be updated since we place the device in a remote area, so if possible i want to display the latest data instead.

I want to display the newest data in table in a website automatically. I try to open csv file in array using php, but the code only display the first row (oldest data) of csv file while i want to display the last row (newest data).

Code that I have tried

$files_location = "database/";

function latest_file_Name ($files_location) {

$openDir = opendir($files_location);

while (false !== ($fileName = readdir($openDir))) {

if($fileName != "." && $fileName != "..")
{
$list[date("YmdHis ", filemtime($files_location.$fileName))]=$fileName;
}
}
rsort($list);
$last_file =array_shift($list);
return $last_file;
}

$last_file_in_folder = latest_file_Name($files_location);
pre_r($last_file_in_folder);//newest file name
$f_pointer=fopen("database/".$last_file_in_folder,"r"); // file pointer
$ar=fgetcsv($f_pointer);
pre_r($ar);//display data of newest file in array

Is there a way to display the newest data using php or any other languange/method? Any help would be appreciated

Here is a link for csv file csv

I've solved the problem, thank you everyone

Community
  • 1
  • 1
Waloy
  • 13
  • 7
  • 1
    You are reading only the first row, so of course that’s what you get. Read all of them then, in a loop - and access the value of the last one afterwards. – 04FS Mar 25 '19 at 09:01
  • Thank you, i understand the concept, I've tried the while loop to read all the array, but i cant access the last row since the array key always reset to 0. – Waloy Mar 25 '19 at 10:24
  • What array key are you talking about? Something simple like `while($line = fgetscv()) { $data[] = $line; }` should get you all the data in an array, so you can pick the last item from that afterwards. (Of course you could go for something a bit more sophisticated than gathering all the data you won’t need in the end, but try and get it running this way first maybe.) – 04FS Mar 25 '19 at 11:10
  • Okay, I'm new to web development, sorry if i don't get the logic. I mean if i want to pick the the last item of multidimensional array, I need the keys (example: [0] for the first array). When i loop the file, the array keys always reset to [0] for each row, so I can't pick the last array. Is there any other way to pick the last item other than using array keys? please help. And I can't show all the data in website since we have around 25 device and will continue to grow. I want to display all the reports in one page. – Waloy Mar 25 '19 at 13:14
  • No it's not a duplicate, I can display the last row if I select the file name. This is a different situation. – Waloy Mar 25 '19 at 13:15

1 Answers1

0

$files_location = "database/";

function latest_file_Name ($files_location) {

$openDir = opendir($files_location);

while (false !== ($fileName = readdir($openDir))) {

if($fileName != "." && $fileName != "..")
{
$list[date("YmdHis ", filemtime($files_location.$fileName))]=$fileName;
}
}
rsort($list);
$last_file =array_shift($list);
return $last_file;
}

$last_file_in_folder = latest_file_Name($files_location);
// print_r($last_file_in_folder);//newest file name
$f_pointer=fopen($files_location .$last_file_in_folder,"r"); // file pointer
while (!feof($f_pointer)) {
    $ar=fgetcsv($f_pointer);
print_r($ar);//display data of newest file in array
}
  • Thank you, this code read all arrays, but is there a way to display only the last array? I can't display it since the array keys reset to 0 for every array. Yesterday I've manage to "loop the while loop" to get array keys indexed, but the code is gone since i just copied from another developer. My bad, I'm new to web development. – Waloy Mar 25 '19 at 10:30
  • welcome @Waloy , if this code works for you then mark it accepted. So, it will improvement for community. – – Bhupendra Dudhwal Mar 25 '19 at 11:35
  • okay thank you for the respon – Waloy Mar 25 '19 at 13:16