完成代码后如何更新数组


How to update the array after finish the code?

超级英雄的所有代码完成后,如何更新我的$superhero_list数组.php,我想搜索另一个名字?

发现的问题是,在我完成超级英雄之后.php并返回到超级英雄.html,它不会将姓氏保存在$superhero_list数组中。

超级英雄.html

 <html>
    <head>
      <title>Superhero List</title>
    </head>
    <body>
      <form method="post" action="superhero.php">
        <label for="heroname">Check The Super Hero Name:</label>
        <input type="text" id="heroname" name="heroname">
      </form>
    </body>
 </html>

超级英雄.php

<?php
  $superhero_list = array();

if (in_array($_POST ["heroname"], $superhero_list)) {
    echo 'Your hero was found.<br>';
    echo "These are the Super Powers:<br> - Invisibility <br> - Xray Vision    <br> - Flight <br> - Underwater Breathing <br> - Immortality <br> - Healing Power <br> 
    - Mind Reading <br> - Supersmart <br> - Strenght<br>";
} else {
    echo "Hero was added to the Super Hero List!";
    array_push($superhero_list,$_POST ["heroname"]);
}

echo '<br><br>';
echo 'This your Hero List:<br>';
echo implode("<br>",$superhero_list);
?>

另一件事,有更好的方法来编写此代码吗?使用函数或其他循环?

提前感谢伙计们!

如果你不想存储在数据库中,那么你需要将数组值存储在cookie中。

http://php.net/manual/en/features.cookies.php

对于cookie,您可以存储价值,直到您的浏览器不会关闭。

每次运行 PHP 脚本时都会重置数组。您需要保存数据,以便下次运行时可以拉回数据。您可以通过构建一个数据库来保存所有名称来执行此操作,也可以将它们保存到文件中。使用这么小的东西将其保存到文件中可能是最简单,最快捷的选择。

将数据保存到文件,请将 php 脚本更改为

<?php
    $superhero_list = array();
    //Load the list from the file
    $filename = 'heroNames.txt';
    //First check if the file exists
    if (file_exists($filename)) {
        //If the file exists load the data
        //First open the file for reading using "r"
        $myfile = fopen($filename, "r") or die("Unable to open file!");
        //Save it into the temp string
        $tempString = fgets($myfile);
        //turn that string into an array using ":" as the seperator. We will save using ":" later
        $superhero_list = explode(":", $tempString);
        //ALWAYS CLOSE THE FILE!!!
        fclose($myfile);
    } 
    //Now the data is either empty since its the first time used or it has all the names of the old superheros
if (in_array($_POST ["heroname"], $superhero_list)) {
    echo 'Your hero was found.<br>';
    echo "These are the Super Powers:<br> - Invisibility <br> - Xray Vision    <br> - Flight <br> - Underwater Breathing <br> - Immortality <br> - Healing Power <br> 
    - Mind Reading <br> - Supersmart <br> - Strenght<br>";
} else {
    echo "Hero was added to the Super Hero List!";
    array_push($superhero_list,$_POST ["heroname"]);
}
//Now to save the data.
//With PHP if you open a file to write and the file does not exist, it will create the file... SO...
//Open the file for writing using "w"
$myfile = fopen($filename, "w");
//Convert the superhero array to a string using ":" to separate them
$tempString = implode(":", $superhero_list);
//Now save that string to the file
fwrite($myfile, $tempString);
//ALWAYS CLOSE THE FILE
fclose($myfile);

echo '<br><br>';
echo 'This your Hero List:<br>';
echo implode("<br>",$superhero_list);
?>

据我了解,您希望:

  • 如果英雄存在,则回显有关英雄的信息。

  • 如果英雄不存在,请将它们添加到数组中。

而且,您希望能够跟踪添加到数组中的每个英雄,即使在用户导航离开并返回之后也是如此。


当您离开 php 文件/页面时,变量/文件/类中的任何数据都将丢失。您必须有一些方法来存储英雄列表(例如数据库/其他形式的存储)。

使用数据库,您将拥有名称/每个特征的字段。当用户提交表单并发送到 superhero.php 文件时,您需要在数据库中查询条目/英雄列表。然后,您将能够检查英雄是否存在。如果英雄存在,则回显英雄字段/数据。如果主条目不存在,请将它们插入到数据库中。

我想另一种选择是将每组数据保存到文本文件中。然后,您必须在每次调用脚本时管理对文件的读取/写入。但是,我不会这样做...