I have a text file while contains a variable amount of lines and each line contains 3 things, an IP, browser info and a date. Basically it's a visitor log that contains these things.
I want to take a line, get each seperate part and replace rows in a html documents table. As of now I'm only able to get the first line from the file.
When I tried to just print something in the while loop that goes through each line to see if it actually goes through it multiple times it doesn't. It only echos the one time, it goes one lap and reads one line and then stops.
Text file contains for example:
Date, browser info and IP.
<?php
// file path
$path = './visitors.txt';
$html = file_get_contents("log.html");
$visitor_log = fopen($path, 'r');
if(flock($visitor_log, LOCK_SH)){
// Loop genom alla rader i visitors.txt
while (($line = fgets($visitor_log)) !== false) {
$html_pieces = explode("<!--==xxx==-->", $html, 3);
$string_split = explode("--", $line, 3);
$html = str_replace("---date---", $string_split[0], $html);
$html = str_replace("---browser---", $string_split[1], $html);
$html = str_replace("---ip---", $string_split[2], $html);
}
fclose($visitor_log);
}else{
die("Error! Couldn't read from file.");
}
echo $html;
?>
I have no idea why the loop doesn't go through the entire file. Is there a way to just echo every line to see if it can actually read all lines?
EDIT: I just tried
echo $html;
in the while loop and it prints out the first line three times so I believe it goes through all three lines but it doesn't get the new data. Does it have something to do with:
$html = file_get_contents("log.html");
not getting updated?
EDIT: html code for the table
<table cellspacing="0" cellpadding="10" border="1" width="100%">
<thead>
<tr>
<th align="left">Dare</th>
<th align="left">Browser</th>
<th align="left">IP</th>
</tr>
</thead>
<tbody>
<!--==xxx==-->
<tr>
<td align="left">---date---</td>
<td align="left">---browser---</td>
<td align="left">---ip---</td>
</tr>
<!--==xxx==-->
</tbody>
</table>
The problem is with how you are adding data to your $html
:
$html = str_replace("---date---", $string_split[0], $html);
This will replace every instance of ---date---
in your $html
string with the date of the line currently being processed. If $html
is a single row template, that means it is converted into a row of data for the first line. That will work fine.
But for the next line, the substitution strings ---date---
etc do not appear in $html
any more - $html
now includes your line 1 data. So the str_replace()
doesn't find anything to replace, and won't actually do anything, and you'll never see anything except the first line.
The best solution would be to keep your template and the generated results separate:
$html = file_get_contents("log.html");
// Split up your html into header, data row, and the bottom
$html_pieces = explode("<!--==xxx==-->", $html, 3);
// Show the table header
echo $html_pieces[0];
$path = './visitors.txt';
$visitor_log = fopen($path, 'r');
while (($line = fgets($visitor_log)) !== false) {
$string_split = explode("--", $line, 3);
// Generate a new line of $output from the data row of your $html_pieces
$output = str_replace("---date---", $string_split[0], $html_pieces[1]);
$output = str_replace("---browser---", $string_split[1], $output);
$output = str_replace("---ip---", $string_split[2], $output);
// And display, line-by-line
echo $output;
}
// All done, finish off your table
echo $html_pieces[2];