开发者

(PHP) Help debug tracking script to show only unique items

i have this script that i am trying to implement to work on my website and the script is suppose to track what games are being played at the moment but the only problem is that when i call the script to display the entries it would show duplicates and i want the script to actually explore the string and take only the $gamename and look for duplicates, not the entire string.

What the script does: Records the gameID, gamename, gamethumb url开发者_如何学编程, IP and time all separated by |. Example: 1744|The Simpliest Snowboarding|The Simpliest Snowboarding|77.88.42.26|1264717552

Look for the IP and if already exists update the record with the new info. If the IP does not exist already write a new line with the information. If the record is older then 60min erase it.

Its just a simple script that i will use to show what people are currently playing on the website.

$dataFile = "visitors2.txt";
$numbergames = 30;
//Please do not edit bellow this line

error_reporting(E_ERROR | E_PARSE);

$users = array();

//getting
$fp = fopen($dataFile, "r");
flock($fp, LOCK_SH);
while(!feof($fp)) {
$users[] = fgets($fp, 4096);
}
flock($fp, LOCK_UN);
fclose($fp);
$i = 0;
echo '<ul>';
foreach(array_unique($users) as $key => $data) {
list($game2id , $gamename , $gamethumb , , ) = explode("|", $data);
//echo $game2id . $gamename;
if($gamename != "" && $i <= $numbergames) {
$newpageurl = str_replace(" ", "-", strip_tags(trim(str_replace($rplce, "", $gamename))))  ;
$url = $game2id .'-'. $newpageurl .'.html';

echo '<li><a href="'.$url.'"><img src="./arcade/img/'.$gamethumb.'.png" width="35" height="35" border="0" /></a>'.$gamename.'</li>';
}
$i++;

}

Please, help and thanks everyone in advance.


Your array_unique() call is only eliminating duplicate rows - entire rows, not just game names. You need to create an array of just the game names. Then, you can eliminate the dupes.

Something like:


$currentGames = array();
$max = 30;
$fp = fopen($dataFile, "r");
flock($fp, LOCK_SH);
while ((count($currentGames) < $max) && (($data = fgetcsv($fg, 0, '|')) !== FALSE) 
{
 if (!in_array($data[1], $currentGames)) $currentGames[$data[0]] = $data[1];
}

This will give you an associative array of unique game names with game ids as keys.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜