Forum Moderators: coopster

Message Too Old, No Replies

Aggregating data from a text file with PHP

Like log files, etc.

         

MatthewHSE

9:16 pm on Apr 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've got a small search engine on my site that is basically used for retreiving specific documents. Folks can enter a number into the search box and it automatically takes them to the result they're looking for. The search script keeps a log file of the search "terms," but it's in a straight text file with no easy way of analyzing it.

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

mcibor

9:37 pm on Apr 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



First of all retrieve the search term. Either by substr, or by explode by ;. Then I would create an array count and would increment relevant field:

$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

ironik

2:18 am on Apr 29, 2005 (gmt 0)

10+ Year Member



Not sure if you might find this useful. I modified something I had already as an example, you can put your array processing logic in the process() method ...


<?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
?>