Reputation: 43
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!
Upvotes: 0
Views: 97
Reputation: 935
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);
?>
Upvotes: 0
Reputation: 275
If you dont want to store in database then you need to store array value in cookie.
http://php.net/manual/en/features.cookies.php
For cookie you can store value until your browser will not close.
Upvotes: 1
Reputation: 193
To my understanding you want to:
If the hero exists, echo the information about the hero.
If the hero does not exist, add them to the array.
And you want to be able to keep track of every single hero that is added to the array, even after the user navigates away and back again.
When you navigate away from the php file/page, any data within the variables/file/class is lost. You would have to have some method to store the list of heros (Like a database/some other form of storage).
With a database, you would have fields for the name/each trait. When the user submits the form and sent to the superhero.php file, you would need to query the database for a list of entries/heros. Then you would be able to check if the hero exists or not. If the hero exists, echo that heros fields/data. If the hero does not exist, insert them into the database.
I guess another option would be to save each set of data to a text file. Then you would have to manage reading/writing to the file each time the script is called. However, I wouldn't do it this way...
Upvotes: 0