0

well I have a search feature on my site and it works great if the query returns results but if it doesn't it falls apart what do I do to make it say "no search results found" when the query comes up as empty

My code is this to handle it and I think I'm colse but its still not working quite right

<?
REQUIRE_ONCE $_SERVER['DOCUMENT_ROOT'] . '/hospital/includes/helpers.inc.php';
include $_SERVER['DOCUMENT_ROOT'] . '/hospital/includes/connectdb.inc.php';
$term = sanitize($_POST['search-term']);
$sql = "select name, id, photo, parents from babies where babies.name like '%" . $term . "%'  or babies.parents like '%" . $term . "%' ORDER BY name DESC ";
$result = mysqli_query($link, $sql);

$row = mysqli_fetch_array($result);

if($row[0] > 0)
{
while($query = mysqli_fetch_array($result))
{
$babies[] = array('name' => $query['name'], 'id' => $query['id'], 'photo' => $query['photo'], 'parents' => $query['parents']);

}
}
$sql2 = "select name, id, photo, title, info from providers where providers.name like '%" . $term . "%' ORDER BY name DESC ";
$result2 = mysqli_query($link, $sql2);
$row2 = mysqli_fetch_array($result2);
if($row2[0] > 0)
{

$result2 = mysqli_query($link, $sql2);
while($query2 = mysqli_fetch_array($result2))
{
$providers[] = array('name' => $query2['name'], 'id' => $query2['id'], 'photo' => $query2['photo'], 'title' => $query2['title'], 'info' => $query2['info']);
}
}
include 'search.html.php';
?>

as you can see if it comes up with no rows it just comes up false but my problem lies in how to output that into the search template
this is what it looks like right now

<html>
<head>
</head>
<body>

<? foreach($babies as $baby):?>
<?echo $baby['name']; ?> and <? echo $baby['parents']; ?>
<br>
<?endforeach ;?>
<? foreach($providers as $provider):?>
<?echo $provider['name']; ?>
<br>
<?endforeach; ?>

</body>
</html>

what do I do to check if its receiving no variables from the controller script and just echo "no search results found"

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by pritaeas
0

Can't you just put an if statement around your foreach ? Something like:

if ($babies) {
  foreach ($babies as $baby) {
    echo $baby['name'] . ' and ' . $baby['parents'] . '<br/>';
  }
}
else {
  echo 'No results.<br/>';
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.