I am working on a project where my script needs to allow the staff to enter a performer/band name into a form and see the correct corresponding phone number when the form is submitted.
The text file I have been given is as follows:
Spiders 0392848271
TheBESTband 0482947291
Lauren 0382718319
Queen 0482827183
KateIsland 0373829420
Reposessed 0472478292
HighRollers 0289428372
Mindstorm 0347287492
WhirlyBirds 0272729193
Traceylyly 0303828294
I have already written the code to parse the text file and store the values into an associative array, but I need to know how to get input from the form and match it to the the associative array and print the corresponding phone number.
<?php
$filename = "performers.txt";
$myfile = fopen($filename, "r") or die("Unable to open file!");
$content = array();
while(!feof($myfile)) {
$content[] = fgets($myfile);
}
fclose($myfile);
$data = array();
foreach($content as $line) {
$pieces = explode(' ', $line);
$data[trim($pieces[0])] = trim($pieces[1]);
}
foreach ($data as $name => $phoneno) {
if (isset($_POST['performer']))
{
$performer = $_POST['performer'];
foreach ($data as $name => $phoneno)
{
if ($name == $performer)
{
echo $phoneno;
}
}
}
}
My html coding:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<h1>Melbourne Performance Hall <br> Performer Phonebook </h1>
<p> Enter the performer or band name with no spaces <br> If found, the phone number will be displayed.</p>
<form action="findPerformerD.php" method="get">
<fieldset>
<p align="left">
<label> Performer/ band name: </label>
<input type="text" name="performer" size="50"> <br>
<input type="submit" name="submit" value="Submit performer name"> </p>
</fieldset>
</form>
</body>
</html>
This is what I have done, however when I try the html form, all it does is just give me a white page.
3
Answers
I have found the answer and managed to debug it finally :). if anyone wanted to see my code this is what I have came up with. Thank you for all the contributions.
Firstly, your form submits via GET not POST, so nothing in
$_POST
will be populated.You can change
to
to resolve that.
Secondly, you are looping through the
$data
array twice for no apparent reason. This isn’t helping you to find the matching data.In fact though you don’t even a loop at all to do the test:
. Instead you can try to access the performer record directly – that’s what’s useful about using an associative array with the performer’s name as the index.
In your PHP, replace the whole of the
foreach ($data as $name => $phoneno) { ... }
section with:Therefore if you were, for example, to input
Lauren
into the form, you’d expect it to output "Phone number is: 0382718319"PHP has a native function to help you open a file and parse formatted lines.
fscanf()
looks perfect for the task; this will eliminate that extra loop and more directly generate the desired lookup array.As a general rule, when you are submitting data to a script that is going to "write" to the server (affect the database or filesystem), use
POST
. When only "reading" from the server, useGET
.In your case,
GET
seems appropriate to me.Using a lookup and searching for keys will ALWAYS be faster than making value searches in PHP.
You might only bother to read and parse your file if
isset($_GET['performer'])
is actuallytrue
.