dongqing344716 2016-03-03 04:37
浏览 46
已采纳

完成代码后如何更新数组?

How can I make my $superhero_list array updates after all the code on the superhero.php is done and I want to search for another name?

The problem I find is that after Im done with the superhero.php and go back to superhero.html, it doesnt save the last name on the $superhero_list array.

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

?>

Another thing, there is any better way to write this code? With functions or other loops?

Thanks in advance guys!

  • 写回答

3条回答 默认 最新

  • dongyong5255 2016-03-03 05:20
    关注

    You are resetting the array every time you run the PHP script. You need to save the data so that next time it runs it can pull the data back. You can either do this by building a database to hold all the names, or you can save them to a file. With something this small saving it to a file is probably the easiest and quickest option.

    To save the data to a file change your php script to

    <?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);
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 ESP32使用MicroPyhon开发,怎么获取485温湿度的值,温湿度计使用的鞋子是Modbus RTU
  • ¥50 苹果MGIE项目部署缺少emb权重
  • ¥15 采用ansys进行机翼在特定路径下的打孔过程中的受力分析
  • ¥15 单片机adb主机连接手机,usb调试密钥无法保存
  • ¥15 已知X和Y有以下关系,求X和Y的关系式
  • ¥15 net core 同时编辑怎么防止数据多保存了
  • ¥15 matlab做ba模型让其在ba和er规则下生长
  • ¥15 请问Quartus的Verilog代码怎么写?
  • ¥100 Mac 版foxmail 收邮件问题
  • ¥15 QWebEngineView