<form action="" method="POST">
<label for="search-terms">enter search terms</label>
<input type="text" name="search-terms" id="search-terms" value=''>
<input type="submit" name="submit" value="Search"/>
</form>
<?php
// Comment out PHP error reporting
// ini_set('display_errors', 1);
// ini_set('display_startup_errors', 1);
// error_reporting(E_ALL);
$images = [];
$search_terms = [];
$termsWithoutResult = [];
if ($_SERVER['REQUEST_METHOD'] === 'POST':wink: {
if ($_POST['search_terms']) {
$original_search = $_POST['search-terms'];
$altered_search = strtolower($original_search);
$altered_search = trim($altered_search);
$search_terms = explode(" ", $altered_search);
$search_terms_amount = count($search_terms);
}
function doSearch($terms) {
$directories = ["img/", "test/"];
$filetypes = ["jpg", "jpeg", "bmp", "png", "gif"];
if (count($terms) === 0) {
return [];
}
$output = [];
foreach ($directories as $dir) {
$iterator = new DirectoryIterator($dir);
foreach ($iterator as $file) {
if ($file->isDot()) {
continue;
}
$name = strtolower($file->getFilename());
$extension = strtolower($file->getExtension());
$term = getTermMatchingFilename($name, $terms);
if ($term !== false && in_array($extension, $filetypes, true)) {
$output[$term] = [
'original_name' => $name,
'extension' => $extension
];
}
}
}
return $output;
}
function getTermMatchingFilename(string $filename, array $terms) {
foreach ($terms as $term) {
if ($filename === $term || strpos($term, $filename) !== false) {
return $term;
}
}
return false;
}
$images = doSearch($search_terms);
$termsWithoutResult = array_filter($search_terms, function ($term) use ($images) {
return array_key_exists($term, $images);
});
}
?>
<?php if (count($termsWithoutResult)): ?>
<?php printf('No results for the following term(s): %s', implode(', ', $termsWithoutResult)); ?>
<?php endif; ?>
<?php foreach ($images as $term => $files): ?>
<?php printf('%d results for %s', count($files), $term); ?>
<?php foreach ($files as $file): ?>
<div class="search-result">
<div class="img-result">
<img src="<?php echo $dir . $file['original_name']; ?>">
</div>
<div class="title-result">
<p><?php echo $file['original_name']; ?></p>
</div>
<br>
</div>
<?php endforeach; ?>
<?php endforeach; ?>
I did a couple things here - first off, since you're always referencing the $images and $search_terms variables in your output, I added some defaults for those for cases where the page is first requested, which will be a GET request usually.
When it is a POST request, we'll first check for the existence of the search_terms field just to be sure it exists. If so we'll go ahead with unpacking that string into an array.
Then the bulk of the logic's been moved into the doSearch() function. It uses something called a guard clause to return early if for some reason we got something like an empty string and there are no terms in the array. In that case it just gives you an empty array - if there are no terms to search, there's no images to match.
Then it goes on to loop over all the directories you've specified. I removed the is_dir() check because presumably since you control the array of directories, these should always exist. If they might not always exist though, then that check can go back up to the beginning of the foreach loop.
Next we create a DirectoryIterator and loop over it. If we're dealing with a dot file, we skip to the next one - in case you're unfamiliar, these files are something in UNIX filesystems, basically it is a reference to the current directory (.) and the parent directory (..).
Otherwise we'll get the name and extension similar to how you did before, and check if the name and extension belong to the terms and extensions arrays respectively. If so we add the current file to our output array.
Finally once we've iterated over everything, it returns the output array. So whenever a POST request is made we'll call this function and get an array of some sort.
Haven't tested this code yet so let me know if you run into any errors and I'm happy to help or fix anything I might have foobared in the process. It's still pretty early so I might've missed something.
edit: Made some more changes, mainly around filename matching and how the "number of results" or "there are no results for blank" bits are output.