Okay everyone. The goal was to have a search box that the user could either search a postcode or a location.
The database was set out like so:
location_id location_name location_postcode
1 bristol bs
2 glasgow g
Some postcodes began with with only one letter and some started with two, like in the database example above. Now the code below I changes the $location_search variable to either the first letter if it's a postcode with only one letter, or two letters if its a postcode with two letters. Now if its a location such as Bristol. It'll ignore the postcode and assign the entire word to the variable $location_search.
Now I just finished this code. It could be better but It worked. Once it's assigned to the variable you can use it to run mysql queries.
<?php //Assign location ID
$location_word = mysql_real_escape_string($_POST["location_input"])
if(!empty($location_word)){
//Remove spaces
$location_word = str_replace(" ", "", $location_word);
//Work out if it's a postcode or a location.
$location_3rd_character = substr($location_word, 2, 1); //Get third character
if(!ctype_alpha($location_3rd_character)){ //Check if it's a number or not
$location_type = "postcode";
}
else {
$location_type = "city";
}
if($location_type == "postcode"){ //Definding the postcode search, strip down to first or second letter.
$location_2nd_character = substr($location_word, 1, 1);
if(!ctype_alpha($location_2nd_character)){ //Trim postcode down to one letter, if only one letter in postcode.
$location_search = substr($location_word, 0, 1);
}
else { //Trim postcode down to two letters, if only two letter in postcode.
$location_search = substr($location_word, 0, 2);
}
}
else {
$location_search = $location_word;
}
?>