【问题标题】:How to update the array after finish the code?完成代码后如何更新数组?
【发布时间】:2016-03-03 04:37:03
【问题描述】:

在 superhero.php 上的所有代码完成后,我如何更新我的 $superhero_list 数组并且我想搜索另一个名称?

我发现的问题是,在我完成 superhero.php 并返回 superhero.html 后,它没有将姓氏保存在 $superhero_list 数组中。

superhero.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>

superhero.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);

?>

另外,有没有更好的方法来编写这段代码?有函数还是其他循环?

提前谢谢各位!

【问题讨论】:

    标签: php html arrays forms


    【解决方案1】:

    如果您不想存储在数据库中,则需要将数组值存储在 cookie 中。

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

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

    【讨论】:

      【解决方案2】:

      每次运行 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);
      
      ?>
      

      【讨论】:

      • 你能解释一下这部分吗: //将超级英雄数组转换为字符串,使用“:”分隔它们 $tempString = implode(":", $superhero_list);为什么:?
      • 如果您希望保存的不仅仅是他们的名字,您应该查看 json_encode 和 json_decode。它将适用于我编写的脚本并稍作调整。
      • 包含超人蜘蛛侠和蝙蝠侠的数组将被转换为字符串“超人:蜘蛛侠:蝙蝠侠”。我这样做是为了可以将其写入文本文件
      【解决方案3】:

      据我了解,您希望:

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

      • 如果英雄不存在,则将其添加到数组中。

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


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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-11-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多