我正在尝试编写一个函数,为玩家编写一个分数列表。PHP:文件写入问题
例如:
player_1 100 12 12 10
player_2 39 13 48 29
而当玩家击败(或做更糟),比他们以前的成绩,他们的成绩是过度写入新成绩。
我已经写了一个功能,这种工作,但有多个问题。
function write($player)
{
global $logfile;
$lines = file($logfile);
foreach($lines as $i => $line)
{
$pieces = explode(" ", $line);
$pieces[0] = trim($pieces[0]);
if($pieces[0] == $player->name) //found name
{
trim($lines[$i]);
unset($lines[$i]); //remove the old player data
$lines[$i] = "{$player->name} {$player->lvl} {$player->exp} {$player->mana} \n"; //write the new score
$fp = fopen($logfile,'a');
fwrite($fp,$lines[$i]);
$found = TRUE;
break;
}
}
if(!$found) //record a new player whose score isn't in the file
{
$fp = fopen($logfile,'a');
$newp = "$player->name $player->lvl $player->exp $player->mana \n";
fwrite($fp, $newp);
}
fclose($fp);
}
该文件只是附加新分数并且不会覆盖以前的分数。有人能指出我的错误吗?
啊,我现在明白了!谢谢一堆! – dukevin 2012-01-28 21:10:34