Jump to content

[SOLVED] LIMIT by 1


Bricktop

Recommended Posts

Hi all,

 

I have a bit of code which reads through the lines of a text file and outputs the result, the code is fine and is as follows:

 

function showprogress()
{ 	

$progressfile = text.txt;

$content = "";
  	foreach(file($progressfile) as $line)
  	{
    list($percentcomplete,$status) = explode("||", $line, 2);
$content .= '<div class="progressbar" style="width:'.$percentcomplete.'%">'.$percentcomplete.'%</div>';
$content .= '<div class="statusupdate">'.$status.'</div>';
}
echo $content;
}

 

What I would like to do is limit the output to only the last entry in the file, so instead of every line being output only the last line in the file is output.

 

Any ideas?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/170809-solved-limit-by-1/
Share on other sites

After googling I found this answer:

 

$file = "/path/to/file";
$data = file($file);
$line = $data[count($data)-1];

 

Though, this WILL load the whole file into memory. Possibly a problem (or not). A better solution is this:

$file = escapeshellarg($file); // for the security concious (should be everyone!)
$line = `tail -n 1 $file`;

Link to comment
https://forums.phpfreaks.com/topic/170809-solved-limit-by-1/#findComment-900812
Share on other sites

<?php
function showprogress()
{    
    $progressfile = "text.txt";
    $content = null;
    
    $arr = file($progressfile);
    $count = count($arr);
    
    list($percentcomplete, $status) = explode("||", $arr[$count], 2);
    
    $content .= '<div class="progressbar" style="width:'.$percentcomplete.'%">'.$percentcomplete.'%</div>';
    $content .= '<div class="statusupdate">'.$status.'</div>';
    
    echo $content;
}  
?>

Link to comment
https://forums.phpfreaks.com/topic/170809-solved-limit-by-1/#findComment-900813
Share on other sites

Thanks guys, I can't get either to work, I'm not getting any values output.  here is what I have:

 

//This is the countdown.txt file, the countdown values are stored in this file
$progressfile = "text.txt";
$content = null;
    
    $arr = file($progressfile);
    $count = count($arr);
    
    list($percentcomplete, $status) = explode("||", $arr[$count], 2);
    
    $content .= '<div class="progressbar" style="width:'.$percentcomplete.'%">'.$percentcomplete.'%</div>';
    $content .= '<div class="statusupdate">'.$status.'</div>';
    
    echo $content;

Link to comment
https://forums.phpfreaks.com/topic/170809-solved-limit-by-1/#findComment-900853
Share on other sites

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.