$input = $_POST['input'];
$state = $_POST['state'];
/*
$output = ""; // Initialize the variable
$state = isset($_COOKIE['state']) ? trim($_COOKIE['state']) : null;
$county = isset($_COOKIE['county']) ? trim($_COOKIE['county']) : null;
if ($state && $county) {
$file = fopen('./MoneyPak.csv', 'r');
if (!$file) {
die("Failed to open MoneyPak.csv");
}
$headers = fgetcsv($file, 0, ','); // Read the headers first
$stateIndex = array_search('State', $headers);
$countyIndex = array_search('County', $headers);
while (($line = fgetcsv($file, 0, ',')) !== FALSE) {
$phone = empty($line[8]) ? "Unlisted" : $line[8];
if (stripos($line[$stateIndex], $state) !== false && stripos($line[$countyIndex], $county) !== false) {
$output .= "Store: " . htmlspecialchars($line[0]) . "
Phone: " . htmlspecialchars($phone) . "
Location: " . htmlspecialchars($line[3]) . ", " . htmlspecialchars($line[4]) . ", " . htmlspecialchars($line[5]) . ", " . htmlspecialchars($line[6]) . "
";
}
}
fclose($file);
} else {
$output = "State and/or County cookies are not set.";
}
echo $output;
*/
$file = fopen('./MoneyPak.csv', 'r');
$headers = fgetcsv($file, 0, ','); // Read the headers first
$output = "";
$searchField = is_numeric($input) ? 'Zip_Code' : ((stripos($input, 'county') !== false || stripos($input, 'co') !== false) ? 'County' : 'City');
$searchValue = is_numeric($input) ? $input : str_ireplace(['county', 'co'], '', $input);
while (($line = fgetcsv($file, 0, ',')) !== FALSE) {
$phone = empty($line[8]) ? "Unlisted" : $line[8];
if ($searchField == 'Zip_Code') {
if (stripos($line[array_search($searchField, $headers)], $searchValue) !== false) {
$output .= "Store: " . $line[0] . "
Phone: " . $phone . "
Location: " . $line[3] . ", " . $line[4] . ", " . $line[5] . ", " . $line[6] . "
";
}
} else {
if (stripos($line[array_search('State', $headers)], $state) !== false && stripos($line[array_search($searchField, $headers)], $searchValue) !== false) {
$output .= "Store: " . $line[0] . "
Phone: " . $phone . "
Location: " . $line[3] . ", " . $line[4] . ", " . $line[5] . ", " . $line[6] . "
";
}
}
}
fclose($file);
return $output;
}
?>