代码只在第二次点击后工作,简单的php

代码只在第二次点击后工作,简单的php,php,Php,我正在尝试用php创建一个简单的程序,它读取包含歌曲的文本文件,然后在屏幕上用每首歌曲旁边的删除按钮将其打印出来,然后当您单击删除按钮时,歌曲应该删除。新文件将写入txt文件,并应在屏幕上刷新。我的代码仅在第二次单击后才起作用。第一次单击实际上会修改txt文件,但屏幕上的代码不会刷新 多谢各位 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>My M

我正在尝试用php创建一个简单的程序,它读取包含歌曲的文本文件,然后在屏幕上用每首歌曲旁边的删除按钮将其打印出来,然后当您单击删除按钮时,歌曲应该删除。新文件将写入txt文件,并应在屏幕上刷新。我的代码仅在第二次单击后才起作用。第一次单击实际上会修改txt文件,但屏幕上的代码不会刷新

多谢各位

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>My Music</title>
<body>
<h1>My Music</h1>
<p>My Library</p>

<?php
//read file
$myFile = "test.txt";
//file to array
$lines  = file($myFile);
//set array length
$length = count($lines);
//print array on screen with delete button
for ($i = 0; $i < $length; $i++) {
    echo $lines[$i] . '<form action="index.php" method="post">
                     <button type="submit" name="test" value="' . $i . '">Delete</button><br><br></form>';
}


//after clicking delete box remove song from array 
if (isset($_POST['test'])) {
    //getting song number to remove
    $songNumber = $_POST['test'];
    //removing song from original array  
    array_splice($lines, $songNumber, 1);
    //open test.txt again
    $myFile2 = fopen("test.txt", "w") or die("Unable to open file!");
    //set updated array length
    $length = count($lines);
    //write array to test.txt
    for ($i = 0; $i < $length; $i++) {
        $txt = $lines[$i];
        fwrite($myFile2, $txt);
    }
}
?>
</body>
</html>

查看代码执行操作的顺序。首先,它读取文件并显示数据。然后从文件中删除一行。在文件已经显示之后

只需交换事件的顺序:

if (isset($_POST['test'])) {
    // perform your delete
}

// Display the contents of the file
$myFile = "test.txt";
// etc.
for ($i = 0; $i < $length; $i++) {
    // ...
}
if(isset($\u POST['test'])){
//执行删除操作
}
//显示文件的内容
$myFile=“test.txt”;
//等等。
对于($i=0;$i<$length;$i++){
// ...
}

您正在读取文件,然后删除标题,当您单击按钮时,您将读取未修改的版本,然后对其进行修改,这就是为什么它在第二次刷新时起作用。。或者单击。。更改代码的顺序

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>My Music</title>
<body>
<h1>My Music</h1>
<p>My Library</p>

<?php

//after clicking delete box remove song from array 
// remove file first if needed...
if (isset($_POST['test'])) {
    //getting song number to remove
    $songNumber = $_POST['test'];
    //removing song from original array  
    array_splice($lines, $songNumber, 1);
    //open test.txt again
    $myFile2 = fopen("test.txt", "w") or die("Unable to open file!");
    //set updated array length
    $length = count($lines);
    //write array to test.txt
    for ($i = 0; $i < $length; $i++) {
        $txt = $lines[$i];
        fwrite($myFile2, $txt);
    }
}

//read file
$myFile = "test.txt";
//file to array
$lines  = file($myFile);
//set array length
$length = count($lines);
//print array on screen with delete button
for ($i = 0; $i < $length; $i++) {
    echo $lines[$i] . '<form action="index.php" method="post">
                     <button type="submit" name="test" value="' . $i . '">Delete</button><br><br></form>';
}



?>
</body>
</html>

我的音乐
我的音乐
我的图书馆


这是因为您在修改文件数据之前回显了文件数据,因此实际上您落后了1个请求。将
if(isset($\u POST['test']){…}
移动到文件输出上方。在我写完我的答案之前,你发布了你的答案,伙计,你太快了!
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>My Music</title>
<body>
<h1>My Music</h1>
<p>My Library</p>

<?php

//after clicking delete box remove song from array 
// remove file first if needed...
if (isset($_POST['test'])) {
    //getting song number to remove
    $songNumber = $_POST['test'];
    //removing song from original array  
    array_splice($lines, $songNumber, 1);
    //open test.txt again
    $myFile2 = fopen("test.txt", "w") or die("Unable to open file!");
    //set updated array length
    $length = count($lines);
    //write array to test.txt
    for ($i = 0; $i < $length; $i++) {
        $txt = $lines[$i];
        fwrite($myFile2, $txt);
    }
}

//read file
$myFile = "test.txt";
//file to array
$lines  = file($myFile);
//set array length
$length = count($lines);
//print array on screen with delete button
for ($i = 0; $i < $length; $i++) {
    echo $lines[$i] . '<form action="index.php" method="post">
                     <button type="submit" name="test" value="' . $i . '">Delete</button><br><br></form>';
}



?>
</body>
</html>