2014-01-21 40 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