我正在嘗試編寫一個函數,爲玩家編寫一個分數列表。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