Forum Moderators: coopster
The search terms will be fairly limited, each term will return only one document, and each document has only one corresponding search term. I want to be able to tell at a glance how many times each document/search-term has been searched for.
The file is in the following format:
192.168.0.1;2005-03-21 17:24:41;1234;1;1;0.28;
192.168.0.2;2005-04-28 13:39:30;5678;1;1;0.34;
As you can see, it's a one-record-per-line list, with semicolons for delimiters. The first field is the user's IP, the second is the date/time, the third is the search term, and the last three fields deal with result relevancy, time spent on the search, etc. I don't use the last three fields at all.
I want to write a PHP script that will read this file and make a nice little HTML table that I can read in my browser, that lists each search term and says how many times it's been searched for.
I can probably figure out most of the script, but how do I go about getting it to count each search term? I'm guessing it will require some regular-expressions and arrays, but that's only hazy guessing on my part and I'm not quite sure how to do it . . .
Any ideas are welcome.
Thanks,
Matthew
$term = substr($line, 32, strpos($line, ";", 32));//or strpos($line, ";", 33) - 1 - you have to check yourself
$count[$term]++;
If $term = 123 the above code should create $count['123'] = 1; if there was none or increment if it existed. If you run this code through your file you should get the desired effect - in $count you'll have the number of terms searched
foreach($count as $key=>$value) echo "Key $key searched $value times<br>";
Best regards
Michal Cibor
<?php
class logFile
{
var $count;function process($item, $key)
{
list($ip, $date, $term, $rel, $time) = explode(';', $item);
if (array_key_exists($term, $this->count))
{
$this->count[$term] ++;
} else {
$this->count[$term] = 1;
}
}function readLogFile($file)
{
if (is_file($this->file))
{
$lines = array();
$fp = @fopen($this->file, 'r');
while ($line = fgets($fp))
{
$lines[] = $line;
}
array_walk($lines, array($this, 'process'));
}
}function buildTable()
{
foreach ($this->count as $key=>$val)
{
// [... output table items (key = term, val = # of times encountered ...]
}
}
}$log = new LogFile;
$log->readLog('myDir/myLog.txt');
$log->buildTable(); // Build a table from count array
?>