2014-01-21 41 views
0

這是我的代碼,我有用於生成新文件的計數器值。我通過ajax將單位值傳遞給php。每當我改變單位值時,我想重置計數器值。怎麼做?如何重置計數器值?

$v=$_GET['v']; 
     $project=$_GET['p']; 
     $unit=$_GET['u']; 
     echo $unit ; 
     $a_str = array($_POST["content"]); 
     $a_url = $_POST["url"]; 
     $contents = implode(PHP_EOL, $a_str); 
     $filename = file_get_contents($project."/counter.txt"); 
     $counter = file_exists($project.'/counter.txt') ? file_get_contents($project."/counter.txt")+1 : 1 ; 
     $contents .= PHP_EOL . PHP_EOL; 
     file_put_contents($project."/unit-".$unit.'.'.$counter."-".$v.".html",$contents); 
     file_put_contents($project."/counter.txt",$counter); 
+0

你在哪裏更改單位價值? –

+0

我通過UI更改了單位值 – user2653831

+0

代碼中的變化在哪裏? –

回答

0

您可以保持會話中的單位值並與當前值進行比較。如果更改,則重置$counter。然後用下一個測試周期的當前值更新會話值。

在上面的代碼片段中添加下面幾行。在會議

if($unit != $_SESSION["cur_unit"]) 
{ 
    $_SESSION["cur_unit"] = $unit; 
    //reset the counter value here 
} 

if(empty($_SESSION["cur_unit"])) //For first time, 
{ 
    $_SESSION["cur_unit"] = $unit; 
} 

集單元值:不要忘記在這個頁面的開頭添加一行session_start();

+0

我試過它不會爲我工作.. – user2653831

+0

我已經添加了代碼。請檢查 –

+0

謝謝,當我改變單位價值我的會議單位價值和獲得單位價值是相同的,它不workig .. – user2653831