PHP results sorter

Need a simple php program which can parse data from a text file and then sort it.  The text file looks like this:

1) Variables to pull out are $pagename (ie voting2.php) which appears at the front of all lines and $rating (ie 5) which always appears at the last character of a line.  Here is an example of a htext file:

/voting2.php****151.37.44.137####0000%%%%5
/voting8.php****151.37.44.137####0000%%%%4
/voting5.php****151.37.44.137####0000%%%%3
/voting8.php****65.23.254.154####0000%%%%5
/voting4.php****12.45.189.123####0000%%%%5
/voting8.php****12.45.189.123####0000%%%%5
/voting3.php****80.137.57.121####0000%%%%5
/voting4.php****80.137.57.121####0000%%%%1
/voting4.php****68.127.121.101####0000%%%%5
/voting6.php****68.127.121.101####0000%%%%4
/voting4.php****83.117.218.79####0000%%%%5
/voting4.php****80.3.160.6####0000%%%%5
/voting8.php****88.108.83.10####0000%%%%5
/voting1.php****68.52.236.61####0000%%%%4
/voting2.php****68.52.236.61####0000%%%%4
/voting8.php****68.52.236.61####0000%%%%5
/voting6.php****66.90.172.23####0000%%%%5
/voting3.php****66.90.172.23####0000%%%%5
/voting8.php****66.90.172.23####0000%%%%5
/voting1.php****207.200.116.72####0000%%%%5
/voting4.php****12.202.221.79####0000%%%%5
/voting4.php****81.151.210.172####0000%%%%5
/voting7.php****81.151.210.172####0000%%%%3
/voting8.php****81.151.210.172####0000%%%%5
/voting4.php****24.66.94.141####0000%%%%4
/voting3.php****24.20.5.66####0000%%%%5
/voting4.php****75.6.195.102####0000%%%%5
/voting8.php****82.36.89.57####0000%%%%3
/voting3.php****68.160.159.71####0000%%%%5

2) Program should then average all of the results for each $pagename
ie
$voting2.php_average = 3.5
$voting3.php_average = 4.8
etc

3) Program should then sort and display the average results to the screen
LVL 2
webcsAsked:
Who is Participating?
 
RoonaanConnect With a Mentor Commented:
Sorry, yes. This scripts just calculates avarages. Better use:

// read file
$lines = file('source.txt');
// Initialize storage
$votes = array();
$sum  = array();

foreach($lines as $line) {
  // Get filename from line
  list($key, $rest) = explode('.php', trim($line),2);
  $key .= '.php';
  // Get last character
  $val = substr($rest, -1);
  // Count the number of votes/hits
  $votes[$key] = isset($votes[$key]) ? $votes[$key] + 1 : 1;
  // Sum the scores
  $sum[$key]  = isset($sum[$key]) ? $sum[$key] + $val : $val;
}

$array = array();
foreach($votes as $key => $count) {
  // Variable is sum/count
  $array[$key.'_average'] = $sum[$key]/$count;
}

arsort($array);

foreach($array as $key => $average) {
  echo '<br/>'.htmlspecialchars($key).': '.$avarage;
}

-r-
0
 
RoonaanCommented:
Hi,

// read file
$lines = file('source.txt');
// Initialize storage
$votes = array();
$sum  = array();

foreach($lines as $line) {
  // Get filename from line
  list($key, $rest) = explode('.php', trim($line),2);
  $key .= '.php';
  // Get last character
  $val = substr($rest, -1);
  // Count the number of votes/hits
  $votes[$key] = isset($votes[$key]) ? $votes[$key] + 1 : 1;
  // Sum the scores
  $sum[$key]  = isset($sum[$key]) ? $sum[$key] + $val : $val;
}

foreach($votes as $key => $count) {
  // Variable is sum/count
  ${$key.'_average'} = $sum[$key]/$count;
}

-r-
0
 
webcsAuthor Commented:
Does this just count the results or average them?  

Also there seems to be no routine to output to the web?

thanks
0
 
webcsAuthor Commented:
Wow works terrific.  Only problem is can it be set to only count lines beginning with /v and ignore all others.

Cause there is some bad data on some lines.  Sure you only have to alter the explode line?

That would be it though.

TO those who might also be using this there is a typo average on the last line.  Other than that its brilliant.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.