2012-02-16 6 views
2

私は本当にPHPに新しいです。私は見たスクリプトに基づいてカウンターを作ることにしました。私はそれに変更を加えました。私はカウンタをリセットする方法を理解しようとしています。PHPのカウンタをリセットするのに助けが必要

$userCount = file_get_contents("count.txt"); 

$userCount = trim($userCount); 

$userCount = $userCount + 1; 

$countReset = $userCount - $userCount; 

$file = fopen("count.txt","w+"); 

fwrite($file,$userCount); 

fclose($file); 

print "The number of visitors is: $userCount"; 

if ($userCount < 20){ 
echo 'not yet'; 
} 
else { 
echo 'done!'; 
} 

if ($userCount > 40){ 
fwrite($file,$countReset); 
fclose($file); 
} 

私はしかし、動作するようには思えないバック0

$countReset = $userCount - $userCount; 

にそれを得るために自分自身からカウンターを引いてみました。カウンタ自体は機能しますが、0にリセットすることができません。

これはPHPを学ぶ方法としてやっているような印象です。また、このポストエディタに苦労してフォーマットを残念に思っています。

スクリプトに関するヘルプはありますか?

+1

すべての初心者へのアドバイス:まず自分のプログラムで何をすべきかと思って、それからステップとチェックを行い、最後にコードを書くように座ります。 –

答えて

1

は、VARに単純に設定された値を0にしてみてください。

$countReset = 0; 
2

あなたは再びそれに書き込みをしようとする前に、ファイルを閉じました。あなたの第二fwrite前に、追加します。

$file = fopen("count.txt","w+"); 
+0

それは、ありがとう。 –

1

は、あなただけのcount.txtファイルを編集することができませんでしたか? PHPでそれをやって

、あなたが

fwrite($file,'0'); 

EDITを行うことができますようCanSpiceは、あなたがそれで終わった前に、あなたはファイルを閉じるべきではない、と述べました。最初のfcloseを削除すると正常に動作するはずです。

1
/**************************************************************************** 
* read the current count from the counter file, increment 
* it by 1, and re-save the new value off to the file 
***************************************************************************/ 
function getAndIncrementCount(){ 
    // attempt to open the file 
    // a+ means keep the contents so we can read it at least once, 
    // but allow us to overwrite the value once we increment it 
    if (($fHandle = fopen('count.txt','a+')) !== FALSE){ 
    // read in the count (also cast to an int so if there is 
    // an invalid (or absent) value it will default to a 0 
    $count = (int) fread($fHandle, 100); 

    // increase the counter 
    $count++; 

    // go back to the beginning of the file 
    fseek($fHandle, 0); 

    // re-write the new count back to the file 
    fwrite($fHandle, $count); 

    // cose the file now that we're done with it 
    fclose($fHandle); 

    // return back the count 
    return $count; 
    } 

    // we couldn't get to the file, so return an error flag 
    return FALSE; 
} 

/**************************************************************************** 
* write the specified value to the counter file 
***************************************************************************/ 
function setCount($count = 0){ 
    // attempt to open the file with over-write permissions 
    // w+ will open the file and clear it 
    if (($fHandle = fopen('count.txt','w+')){ 
    // write the counter to the file 
    fwrite($fHandle, $count); 

    // close the file now that we're done 
    fclose($fHandle); 

    // return the newly saved count 
    return $count; 
    } 

    // we couldn't get to the file, so return an error flag 
    return FALSE; 
} 

、実際に適用される:

$userCount = getAndIncrementCount(); 
echo "The number of visitors is: {$userCount}"; 

if ($userCount < 20){ 
    echo "Not Yet"; 
}else{ 
    echo "Done!"; 
} 
if ($userCount > 40){ 
    setCount(0); 
} 
0

$ countResetがすでにコンテンツに付加されますので、あなたは、ファイルの内容を書き換えていますがfwriteの二度目を使用する場合、それらに追加されていないためですファイル。これを試してみてください:

$userCount = file_get_contents("count.txt"); 
$userCount = $userCount + 1; 

$countReset = $userCount - $userCount; 

file_put_contents("count.txt", $userCount); 

print "The number of visitors is: $userCount\n"; 

if ($userCount < 20){ 
    echo 'not yet'; 
} 
else { 
    echo 'done!'; 
} 

if ($userCount > 40){ 
    file_put_contents("count.txt", $countReset); 
} 
+0

これは私にとってもうまくいきました。 –

1

私は、この文脈でfopen()file_get_content()機能を混ぜ、どちらかfopen()を使用し、fread()fwrite()またはfile_get_contents()file_put_contents()を使用することはありません。

あなただけのカウンタをリセットし、前の値を必要としない、使用よりも、必要がある場合:

file_put_contents('count.txt', '0'); 

あなたは更新値が必要な場合は、いずれかを使用可能性があります

$count = file_get_contents('count.txt'); 
$count++; 

// Reset? 
if($reset){ 
    $count = 0; 
} 

file_put_contents('count.txt', "$count"); 

というか:

$fp = fopen('count.txt', 'r+') or die('Cannot use counter'); 
$count = trim(fread($fp, 1024)); 
$count++; 

// Reset? 
if($reset){ 
    $count = 0; 
} 

ftruncate($fp, 0); 
fseek(0, SEEK_SET) 
fwrite($fp, "$count"); 
fclose($fp); 

ここにはftruncate()fseek()のマニュアルページがあります2つのスクリプトが同時にコンテンツを上書きしないように、flock()を勉強してください。

関連する問題