Info about the function: Currently it grabs last 20 lines and display info based on the requested columns from the lines.
What I want to do
I want to ignore lines that contain same column 5 and 13. Example: (The both lines below should be ignored because column 5 in the first line matches column 13 and the second line is the same as the first one because first name from column 5 matches the name of the column 13)
1,42,16, 201,stackoverflow_user, 1, 6762160, 39799, 9817242, 6762160, 39884, 10010545,stackoverflow_user, 2, 1351147, 1165, 483259, 1351147, 1115, 241630, 0
1,46,27, 201,[stackoverflow_user | stackoverflow_userother], 1, 4078465, 286991, 1594830, 4078465, 287036, 1643156,stackoverflow_user, 2, 1357147, 1115, 241630, 1357147, 1065, 120815, 0
So, the script should grab additional line and ignore the line that columns match until it can display 20 valid lines.
This below is my actual function:
function DMMRankings()
{
# read a file into an array
$lines = file('C:/path/to/file.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
# take the last 20 lines of the file -- i.e. the last 20 values in the array
$last_ten = array_slice($lines, -20);
#create an array for the output
$n = 1;
$content = '';
foreach ($last_ten as $l) {
# treat the data as comma-separated values
$arr = explode(",", $l);
# if col 5 has multiple values, take the first one
if (preg_match("/\[(.+?) \|/", $arr[4], $matches)) {
$arr[4] = $matches[1];
}
# store the data we want in an output array.
$data = array('rank-pos' => $n++, 'rank-name' => $arr[4], 'rank-dmuser' => $arr[12]);
$content .= Template::Load('rankinguserdm-' . ($n % 2 == 1 ? 2 : 1), $data);
}
$this->content = Template::Load('user_rankingsdm', array('rankings' => $content));
}
Please help me out how I can do this on my current function. Thanks!
Here's some code that will test whether col 5 and col 13 are the same, and skip the line if so. It uses an accumulator to keep a count of how many valid lines there are. See the code for comments on what it is doing.
# read a file into an array
$lines = file('C:/path/to/file.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
# flip our array over so the last lines of the file are first.
$lines = array_reverse($lines);
$n = 0;
$wanted = 20; # or however many lines you want.
$content = '';
foreach ($lines as $l) {
# treat the data as comma-separated values
$arr = explode(",", $l);
# if col 5 has multiple values, take the first one
if (preg_match("/\[(.+?) \|/", $arr[4], $matches)) {
$arr[4] = $matches[1];
}
# is arr[4] the same as arr[12]?
if ($arr[4] !== $arr[12]) {
# these two are not equal, so use these values
$data = array('rank-pos' => $n++, 'rank-name' => $arr[4], 'rank-dmuser' => $arr[12]);
$content .= Template::Load('rankinguserdm-' . ($n % 2 == 1 ? 2 : 1), $data);
}
# have we got enough data?
if ($n === $wanted) {
break;
}
}
# check if we got enough data from the loop
if ($n < $wanted) {
die("We couldn't get enough data!");
}
To get 20 valid lines, just keep looping till you have 20.
$lastline=count($lines);
$dsp=0;
while(dsp<20){
//check to see if there are any more lines
if(!$lines[$lastline-$dsp]){
echo 'oops, we ran out of lines';
break;}
$l=$lines[$lastline-$dsp];
# treat the data as comma-separated values
$arr = explode(",", $l);
# if col 5 has multiple values, take the first one
if (preg_match("/\[(.+?) \|/", $arr[4], $matches)) {
$arr[4] = $matches[1];
}
//skip if 5==13
if($arr[4]==$arr[12])continue;
//else: add one to counter
$dsp++;
# store the data we want in an output array.
$data = array('rank-pos' => $n++, 'rank-name' => $arr[4], 'rank-dmuser' => $arr[12]);
$content .= Template::Load('rankinguserdm-' . ($n % 2 == 1 ? 2 : 1), $data);
}