2016-03-03 79 views
0

如何在superhero.php上的所有代碼完成之後,使我的$superhero_list陣列更新並且我想搜索另一個名稱?完成代碼後如何更新數組?

我發現的問題是,在Im完成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); 

?> 

另一件事,有什麼更好的方法來寫這個代碼?有功能或其他循環?

在此先感謝你們!

回答

0

您每次運行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); 

?> 
+0

驚人的哥哥!謝謝! –

+0

你能解釋一下這個部分: //使用「:」將超級英雄數組轉換爲一個字符串以將它們分開 $ tempString = implode(「:」,$ superhero_list); 爲什麼:? –

+0

如果你想保存比他們的名字更多的信息,你應該查看json_encode和json_decode。它將與我通過小調整編寫的腳本一起工作。 –

0

據我瞭解,你想:

  • 如果英雄存在,呼應講述了主人公的信息。

  • 如果英雄不存在,將它們添加到數組中。

而且要能夠跟蹤被添加到陣列中每一個英雄的,用戶導航離開和回來後還是一樣。


當您離開php文件/頁面時,變量/文件/類中的任何數據都將丟失。你將不得不有一些方法來存儲heros列表(如數據庫/其他形式的存儲)。

對於一個數據庫,您將有名稱/每個特徵的字段。當用戶提交表單併發送到superhero.php文件時,您需要查詢數據庫中的條目/英雄列表。那麼你將能夠檢查英雄是否存在。如果英雄存在,請回復英雄字段/數據。如果英雄不存在,則將其插入數據庫。

我想另一種選擇是將每組數據保存到一個文本文件。然後,每次腳本被調用時,您都必須管理讀取/寫入文件。但是,我不會這樣做......