简体   繁体   English

使用 php 覆盖文本文件中的单个值

[英]Overwrite a single value in a text file using php

I'm trying to write a code that is supposed to store a set number of days for an account.我正在尝试编写一个应该为帐户存储一定天数的代码。 The user of this account is then supposed to be capable of selecting a certain timeframe using html.然后,该帐户的用户应该能够使用 html 选择某个时间范围。 Then those days are supposed to be subtracted from the total ammount and the new value is supposed to be put back into the text file.然后应该从总金额中减去这些天数,并且应该将新值放回文本文件中。

So far I've tried solving a lot of this by reading the txt file into an array and working from there, but that's kind of where I run into a dead end cause I can't quite find a solution for my problem.到目前为止,我已经尝试通过将 txt 文件读入数组并从那里工作来解决很多问题,但这就是我遇到死胡同的地方,因为我无法找到解决问题的方法。

The way i have it working at the moment is as such:我目前的工作方式是这样的:

if(isset($_POST['submit_btn'])) //If the submit button is pressed, it starts working through the steps
                {
                    $start1 = date_create($_POST["start"]); //I convert the HTML dates to php dates so i can work with them in a second
                    $end1 = date_create($_POST["end"]);
                    $start2 = date_format($start1, "Y-m-d");
                    $end2 = date_format($end1, "Y-m-d");
                    $end = $end2;
                    $start = $start2;
                    $d_diff1 = date_diff(date_create($end), date_create($start)); //I create the date difference and convert them back into a value I can work with
                    $d_diff = $d_diff1->format('%d days');
                    $up = fopen('text\urlaub.txt', 'a+');

                    if($d_diff < 0) //The next few lines are technically irrelevant for the problem
                    {
                        echo "Ihr Startdatum muss vor dem Enddatum liegen.";
                    }
                    elseif($d_diff == 0)
                    {
                        echo "Sie können heute keinen Urlaub mehr legen.";
                    }
                    elseif($d_diff >= 35)
                    {
                        echo "Ihr Antrag wurde gestellt. Aufgrund der Länge dea Beantragten Urlaubs muss dieser erst manuell bestätigt werden. Sie werden innerhalb von 2 Werktagen benachrichtigt.<br>";
                    }
                    elseif($d_diff >= 1 && $d_diff <= 35) //Here is where the problem starts
                    {
                        echo "Ihr Antrag wurde eingereicht und von System freigegeben. Sollte es Probleme mit ihrem Antrag geben wird sich in den nächsten 2 Tagen ein Mitarbeiter an sie wenden. <br>";
                        $urlaubkey = str_word_count(file_get_contents('text\urlaub.txt'),1,'üöä1234567890-.:;_<>|@€!§%&/=?'); //I read the txt file into the array
                    }
                    $key = array_search($name, $urlaubkey); //I check for the username in the array. If I find it, the array key for the username is saved as a variable
                    echo $key; //Next step would be to increase the value of this by 1 to get the correct key. Then i'd have to overwrite the txt file.

This is basically where I am at now.这基本上就是我现在的位置。 The only other things missing is me increasing the array key by one and me running the simple substraction, but that isn't really the issue唯一缺少的其他事情是我将数组键增加一并且我运行简单的减法,但这并不是真正的问题

The way my txt file saves the values is as such:我的 txt 文件保存值的方式如下:

körner,26
werner,26
albert,30
wernher,26
Walther,34

It's following the scheme of 'Username','Remaining days'它遵循“用户名”,“剩余天数”的方案

Any help would be appreciated任何帮助,将不胜感激

I suggest you serialize and deserialize the contents of the array containing your data using the serialize and unserialize functions;我建议您使用serializeunserialize函数序列化和反序列化包含您的数据的数组的内容; that will make it easier for you to manipulate the contents of the array这将使您更容易操作数组的内容

Please see the following sample code请看下面的示例代码

<?php

define("FILENAME", "C:\TEMP\UserList.txt");

// Create an array containing a list of users 

$userList = array(
    'USER0001' => array(
        'name' => 'Fabio',
        'number' => 23,
    ),
    'USER0002' => array(
        'name' => 'Laura',
        'number' => 7,
    ),
);

// Serialize the array and write the contents to a file 

file_put_contents(FILENAME, serialize($userList));

// Read the contents from the file and deserialize the array 

$userList = unserialize(file_get_contents(FILENAME));

// Update the value of the 'number' field, identifying the user by its unique key 

$userList['USER0001']['number'] = 75;

// Write once more 

file_put_contents(FILENAME, serialize($userList));

// Read once more 

$userList = unserialize(file_get_contents(FILENAME));

// Dump the result 

echo '<pre>';
var_dump($userList);
echo '</pre>';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM