Basically im trying to get my PHP code to search through a txt file which has Surburbs and post codes (3000, Melbourne) etc. The user inputs a suburb and the corresponding post code is printed out.
I think almost all the way through however nothing is printing out. Not really sure why. Asked a few of my web dev friends but they were no help haha. Any help would be greatly appreciated.. Thanks :)
<html>
<body>
<p>INPUT AN SUBURB NAME IN BELOW TEXT BOX AND CLICK 'SEARCH' BUTTON TO SEE
THE RESULT.</p>
<form>
Suburb:<input type="text" name="suburb"/><input type="submit"
value="search"/>
</form>
<?php
if(isset($_GET['suburb']))
{
//load email from file
$file = "postcode.txt";
if(!file_exists($file ))
{
echo "No file found!";
}
else
{
$postcodes=file($file);
for($i=0;$i<count($postcodes);$i++)
{
$postcodes[$i]=str_replace("
","",$postcodes[$i]);
$curPostcode = explode(",", $postcodes[$i]);
if(isset($array_postcode))
{
$array_postcode+=array($curPostcode[1]=>$curPostcode[0]);
}
else
{
$array_postcode=array($curPostcode[1]=>$curPostcode[0]);
}
}
if(isset($array_postcode))
{
echo "nothing"
;}
else {
//print_r($array_postcode);
echo "The postcode of ".$_GET['suburb']." is : "
.$array_postcode['suburb'];
}}
}
?>
</body>
</html>
Assuming your file is in this format:
300, ABC
400, XYZ
500, DEF
you have put a condition of if(isset($array_postcode))
which is true and therefore prints "nothing" in each case. I corrected it if its not set meaning you have nothing in the file then it will print nothing.
Now most importantly. You have to flip the array and pass it the GET variable code in order to map a location against the code. Try the below code.
if(!file_exists($file ))
{
echo "No file found!";
}
else
{
$postcodes=file($file);
for($i=0;$i<count($postcodes);$i++)
{
$postcodes[$i]=str_replace("
","",$postcodes[$i]);
$curPostcode = explode(",", $postcodes[$i]);
if(isset($array_postcode))
{
$array_postcode+=array($curPostcode[1]=>$curPostcode[0]);
}
else
{
$array_postcode=array($curPostcode[1]=>$curPostcode[0]);
}
}
if(!isset($array_postcode))
{
echo "nothing"
;}
else {
$flip_array = array_flip($array_postcode);
if (isset($flip_array[$_GET['suburb']])) {
echo "The postcode of ".$_GET['suburb']." is :" .$flip_array[$_GET['suburb']];
}else{
echo "Not found.";
}
}}
}
I made a function which will return the postcode against a query string. its not sensitive, pass it both upper or lower case letters.
function searchSuburb($array_postcode)
{
$keys = [];
foreach ($array_postcode as $key => $value) {
$keys[] = strtoupper($key);
}
$keys = array_map('trim',$keys);
$stringKeys = array_map('strval', $keys);
$values = array_values($array_postcode);
$array_postcode = array_combine($stringKeys, $values);
$serach_key = isset($array_postcode[strtoupper($_GET['suburb'])]) ? $array_postcode[strtoupper($_GET['suburb'])] : "";
if ($serach_key) {
return "The postcode of ".$_GET['suburb']." is : " .$serach_key;
}else{
return "Not found.";
}
}
Include the function in your file after the php opening tag and call it below.
if(!isset($array_postcode)){
echo "nothing";
}else {
echo searchSuburb($array_postcode);
}