Tuesday, January 25, 2022

[FIXED] Adding "No Results" message when search returns no results

Issue

I need some help adding a "No Results" message to pop up on the page below the search box if no results are found. Any assistance would be appreciated.

Current Code:

<html lang="en">
<head>
<meta charset="utf-8" />
</head>
<body>
<form action="" method="post">  
Search: <input type="text" placeholder="Enter Address" name="term" /><br />  
<br />
<button class="ladda-button btn btn-primary" data-style="zoom-in">Submit
</button>  
</form>  
<?php
    if (!empty($_REQUEST['term'])) 
    {
        $term = mysql_real_escape_string($_REQUEST['term']);     

$sql = "SELECT * FROM triadlocations WHERE address LIKE '%".$term."%'"; 
$r_query = mysql_query($sql); 

    while ($row = mysql_fetch_array($r_query))
    {  
    echo '<br /><br /> Location ID: ' .$row['locationid'];  
    echo '<br /> Address: ' .$row['address'];  
    echo '<br /> City: '.$row['city'];  
    echo '<br /> State: '.$row['state'];  
    echo '<br /> Zip: '.$row['zip'];
    }

}
?>
</body>
</html>

Solution

Check the number of results before your while loop. Print the message if there are no results.

if (mysql_num_rows($r_query)) {
    while ($row = mysql_fetch_array($r_query)){  
    ...
    }
} else {
    echo "No Results";
}


Answered By - Erin

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.