Log in

View Full Version : help with read visits.txt file



vinny.benson
04-01-2010, 07:13 AM
Hi,

On my homepage i have a php script that writes the date (format: d/m/Y) to the text file "visits.txt".

and i want a code on another page that will open the text file and count each date.

e.g.

visits.txt

01/04/2010, 01/04/2010, 01/04/2010, 01/04/2010, 29/03/2010, 29/03/2010

output:

01/04/2010 = 4
29/03/2010 = 2

Nile
04-01-2010, 08:21 AM
Put this on the page you're counting dates:


<?php
$file = fopen('date.txt', 'a');
fwrite($file, " ".date("d/m/Y"));
fclose($file);
?>

Make a blank txt file named date.txt, and then where you want to add up all the dates, put this:

<?php
$file = file_get_contents("date.txt");
$file = explode(" ", $file);
array_shift($file);
$dates = array();
foreach($file as $key => $value){
if(!isset($dates[$value])){
$dates[$value] = 1;
} else {
$dates[$value]++;
}
}
foreach($dates as $key => $value){
echo $key . ' = '.$value .'<br />';
}

vinny.benson
04-01-2010, 08:32 AM
yey, thank you so much that worked perfectly :D:D

vinny.benson
04-01-2010, 10:41 AM
sorry just one more question

in this code, how would I get the date with the highest count



<?php
$file = file_get_contents("date.txt");
$file = explode(" ", $file);
array_shift($file);
$dates = array();
foreach($file as $key => $value){
if(!isset($dates[$value])){
$dates[$value] = 1;
} else {
$dates[$value]++;
}
}
foreach($dates as $key => $value){
echo $key . ' = '.$value .'<br />';
}
?>


basically i want to output this:

01/04/2010 = 4
29/03/2010 = 2

most views on: 01/04/2010 (4)

vinny.benson
04-01-2010, 01:50 PM
doesn't matter, i figured it out



<?php
$max = max($dates);
echo 'most views: ' . $max;
?>

Nile
04-01-2010, 03:14 PM
No problem, glad to help.

It seems your topic is solved... Please set the status to resolved.. To do this:
Go to your first post ->
Edit your first post ->
Click "Go Advanced" ->
Then in the drop down next to the title, select "RESOLVED"