$username = $_GET['username'];
$steps = $_GET['id'];
$myfile = fopen("save/" . $username . ".txt", "w") or die("Unable to open file!");
fwrite($myfile, $steps);
fclose($myfile);
echo $steps;
这是我的代码,问题是它只用$ step替换整个.txt文件,Test1|b7f2b0b64a3c60a367b40b579b06452d|Male|0|a|02/05/2016|1
在文本文件中,因此它最终只是32
{{1在文本文件中,我想将if $steps = 32;
替换为0
提示我知道这样的事情
$steps = $_GET['id'];
答案 0 :(得分:2)
这是因为您要使用$steps
完全替换文件的内容,而$_GET['id']
只包含$username = $_GET['username'];
$steps = $_GET['id'];
$myfile = fopen("save/" . $username . ".txt", "w") or die("Unable to open file!");
//explode on the |
$userData = explode('|', stream_get_contents($myfile));
//This should be the steps data. Replace it with the new value
$userData[3] = $steps;
//put the exploded string back together with the new steps value
fwrite($myfile, implode("|", $userData));
fclose($myfile);
。
如果您确定步骤始终位于同一位置,那么您的爆炸性想法可以为此目的而努力。然后它会有点像这样。
fopen
但是,对于这些小文件, fclose
和$username = $_GET['username'];
$steps = $_GET['id'];
$myfile = file_get_contents("save/$username.txt");
//explode on the |
$userData = explode('|', $myfile);
//This should be the steps data. Replace it with the new value
$userData[3] = $steps;
//put the exploded string back together with the new steps value
file_put_contents("save/$username.txt", implode("|", $userData));
是非常激烈的操作。一种可能更快的方式(也更容易!)是这样做的:
$variables
P.S。您可以在双"
字符串中使用<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example - example-$route-service-production</title>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-route.js"></script>
<script src="script.js"></script>
<script type="text/javascript">
angular.element(document.getElementsByTagName('head')).append(angular.element('<base href="' + window.location.pathname + '" />'));
</script>
</head>
<body ng-app="ngRouteExample">
<div ng-controller="MainController">
Choose:
<a href="Book/Moby">Moby</a> |
<a href="Book/Moby/ch/1">Moby: Ch1</a> |
<a href="Book/Gatsby">Gatsby</a> |
<a href="Book/Gatsby/ch/4?key=value">Gatsby: Ch4</a> |
<a href="Book/Scarlet">Scarlet Letter</a><br/>
<div ng-view></div>
<hr />
<pre>$location.path() = {{$location.path()}}</pre>
<pre>$route.current.templateUrl = {{$route.current.templateUrl}}</pre>
<pre>$route.current.params = {{$route.current.params}}</pre>
<pre>$route.current.scope.name = {{$route.current.scope.name}}</pre>
<pre>$routeParams = {{$routeParams}}</pre>
</div>
</body>
</html>
。
答案 1 :(得分:1)
请注意,您使用的是w
文件模式,该模式会在打开文件时截断该文件。这意味着,如果您没有先将数据放在一边,将丢失数据。根据{{3}}:
仅供写作;将文件指针放在文件的开头,将文件截断为零长度。如果该文件不存在,请尝试创建它。
使用the documentation,您可以先读取数据,然后将整个字符串保存回文件:
$username = $_GET['username'];
$steps = $_GET['id'];
$myfile = fopen("save/" . $username . ".txt", "r+") or die("Unable to open file!");
// Get the current data from the file
$data = fgetcsv($myfile, 0, '|');
// Now replace the 4th column with our steps
$data[3] = $steps;
// Now truncate the file
ftruncate($myfile, 0);
// And save the new data back to the file
fwrite($myfile, implode('|', $data));
fclose($myfile);