I have an array which I have put into a foreach loop, where each value of the array will be outputted to the user. If the user has entered a search query, the value will be checked againast a regex and only be returned if it matches, otherwise the value is just outputted.
The problem I’m having is I havent been able to figure out how to make a conditional “no results found” output if neither the unconditional or the regex conditional outputs output anything. Code below.
foreach ($result as $value)
{
// check to see if query term is set and if so run regex comparison
if (isset($pattern))
{
if (preg_match("/^$pattern/i", $value))
{
echo $value;
echo "<br />";
}
}
// if query is not set, simply output the value
else
{
echo $value;
echo "<br />";
}
// and if there has been no output for either the regex conditional, or other output,
// I want output "no results". How?
}
If I understand correctly this is what you need: