Kevin Tracy
Kevin Tracy

Reputation: 19

Load Individual lines of text from the latest txt file in a directory in php for a website

Every night, a *.txt file is automatically added to an FTP containing the previous day's Fitbit data. The files don't overwrite one another, so we have fitbitdata(1).txt, fitbitdata(2).txt, and so forth. Every now and then, I'll manually clean out the old text files, so it's possible fitbitdata(1).txt may be more recent than fitbitdata(30).txt in some cases.

Anyway, I want to display the data on a website using PHP, but the variables seem broken.

Here's where I'm at now:

<?php 
$fitbit_logs = glob("fitbit/*.*"); 
$no_fitbit_logs = count($fitbit_logs)-0;  
$limit = $no_fitbit_logs-0;
for( $i = $no_fitbit_logs; $i >= $limit; $i--)

$fitbit_file = ".$fitbit_logs[$i].";
$all_lines = file($fitbit_file);
echo "<img src=\"images/steps.png\">".$all_lines[3]." Total Steps
    <br><img src=\"images/floors.png\">".$all_lines[5]." Floors Climbed
    <br><img src=\"images/calories.png\">".$all_lines[7]." Calories Burned
    <br><img src=\"images/elevation.png\">".$all_lines[9]." of Elevation Gained
    <br><img src=\"images/distance.png\">".$all_lines[11]." Traveled on Foot
    <p>Fitbit data from ".$all_lines[1].".</p>"
?>

That code renders everything except the variables:

The results of the above code with everything but the variables

This is based on the PHP I'm already using that pulls the latest image in a directory (or at least I think it's doing that) in combination with CodeSpeedy's code for "How to read a particular line from a text file in PHP." (see below)

<?php
$my_text_file = "mytextfile.txt";
$all_lines = file($my_text_file);
echo $all_lines[2];
?>

I think what the code is doing is using the glob function to search for all files in the directory, count the fitbit files, limit it to the last file, read all the lines from that last file, and then treat each line like as a variable in the echo.

Obviously, I'm doing something wrong. I'm curious to learn how wrong I am. Thanks for your patience, I'm still very new to this!

Upvotes: -1

Views: 28

Answers (1)

Kevin Tracy
Kevin Tracy

Reputation: 19

Just some simple syntax errors! This doesn't display the latest file (like I feared), but it does display the variables for one of the text files in the directory. I'll ask the later part in a new question.

<?php 
    $fitbit_logs = glob("fitbit/*.*"); 
    $no_fitbit_logs = count($fitbit_logs)-1;  
    $limit = $no_fitbit_logs-0;
    for( $i = $no_fitbit_logs; $i >= $limit; $i--)

    $fitbit_file = $fitbit_logs[$i];
    $all_lines = file($fitbit_file);
    echo "<img src=\"images/steps.png\" alt=\"Kevin Tracy Steps\" style=\"padding-right: 10px;\">".$all_lines[3]." Total Steps
        <br><img src=\"images/floors.png\" alt=\"Kevin Tracy Floors Climbed\" style=\"padding-right: 5px;\">".$all_lines[5]." Floors Climbed
        <br><img src=\"images/calories.png\" alt=\"Kevin Tracy Calories Burned\" style=\"padding-right: 5px;\">".$all_lines[7]." Calories Burned
        <br><img src=\"images/elevation.png\" alt=\"Kevin Tracy Elevation Gained\" style=\"padding-right: 5px;\">".$all_lines[9]." of Elevation Gained
        <br><img src=\"images/distance.png\" alt=\"Kevin Tracy Distance Traveled\" style=\"padding-right: 5px;\">".$all_lines[11]." Traveled on Foot
        <p>Fitbit data from ".$all_lines[1].".</p>"
?>

Upvotes: 1

Related Questions