aihao
aihao

Reputation: 13

How do you display a "No Results Found" message (PHP)

Just wondering how I can display this message when I type something in my search bar, and nothing matches what is stored in my MySQL database.

So far what I have is this.

<?php

if(isset($_POST['submit'])){
    $search = trim($_POST['search']);
    if($search != ""){
        //echo "search: ". $search;
        $result = mysql_query("SELECT * FROM catalogue WHERE 
            name LIKE '$name' OR 
            category LIKE '$category' OR
            brand LIKE '$brand' OR
            season LIKE '$season' OR
            price LIKE '$price' OR 
            store LIKE '$store' OR 
            description LIKE '%$search%' ");

        while($row = mysql_fetch_array($result)){
            $name = $row['name'];
            $file = $row['file'];
            $description = $row['description'];
            $category = $row['category'];
            $brand = $row['brand'];
            $season = $row['season'];
            $price = $row['price'];
            $store = $row['store'];
            $cid = $row['cid'];

            echo "\n<div class=\"thumb\">";
            echo "\n\t<a href=\"single.php?cid=$cid\"><img src=\"thumbs/$file\" class=\"thumbnailImg\" width=\"150\" height=\"200\"/></a><br/>";
            echo "\n\t".$name. " ";
            echo "\n\t$". $price;
            echo "\n</div>";
        }//end while loop

    }else{
        echo "<h2><em>No results were found.</em></h2>";
    }//end if search ,else

  }//end if submit
?>

This code snippet works if I just click search without typing anything in, but if I type something in the search that doesn't match up, nothing is displayed. How do I fix that?

Upvotes: 0

Views: 5778

Answers (4)

Giacomo1968
Giacomo1968

Reputation: 26066

Easy. Just take those echo statements & place them in a variable. If the variable is not empty, echo it. If the statement is empty, echo your “No results were found.” message. Adjusted code below:

if(isset($_POST['submit'])){
    $ret = '';
    $search = trim($_POST['search']);
    if($search != ""){
        //echo "search: ". $search;
        $result = mysql_query("SELECT * FROM catalogue WHERE 
            name LIKE '$name' OR 
            category LIKE '$category' OR
            brand LIKE '$brand' OR
            season LIKE '$season' OR
            price LIKE '$price' OR 
            store LIKE '$store' OR 
            description LIKE '%$search%' ");

        while($row = mysql_fetch_array($result)){
            $name = $row['name'];
            $file = $row['file'];
            $description = $row['description'];
            $category = $row['category'];
            $brand = $row['brand'];
            $season = $row['season'];
            $price = $row['price'];
            $store = $row['store'];
            $cid = $row['cid'];

            $ret = "\n<div class=\"thumb\">"
                 . "\n\t<a href=\"single.php?cid=$cid\"><img src=\"thumbs/$file\" class=\"thumbnailImg\" width=\"150\" height=\"200\"/></a><br/>"
                 . "\n\t".$name. " "
                 . "\n\t$". $price
                 . "\n</div>"
                 ;
        }//end while loop

    }

    // Check if '$ret' has content or not.
    if (!empty($ret)) {
        echo $ret;
    }
    else {
        echo "<h2><em>No results were found.</em></h2>";
    }

  }//end if submit

Upvotes: 0

PressingOnAlways
PressingOnAlways

Reputation: 12356

You need to see if any rows were returned. According to the php.net manual:

Use mysql_num_rows() to find out how many rows were returned for a SELECT statement or mysql_affected_rows() to find out how many rows were affected by a DELETE, INSERT, REPLACE, or UPDATE statement.

Something like this would help in your code:

...
$result = mysql_query("SELECT * FROM catalogue WHERE 
            name LIKE '$name' OR 
            category LIKE '$category' OR
            brand LIKE '$brand' OR
            season LIKE '$season' OR
            price LIKE '$price' OR 
            store LIKE '$store' OR 
            description LIKE '%$search%' ");

 if (mysql_num_rows($result) == 0 ) {
    // display no results found
 } else {
    while($row = mysql_fetch_array($result)){
        $name = $row['name'];
        $file = $row['file'];
        $description = $row['description'];
        $category = $row['category'];
        $brand = $row['brand'];
        $season = $row['season'];
        $price = $row['price'];
        $store = $row['store'];
        $cid = $row['cid'];

        echo "\n<div class=\"thumb\">";
        echo "\n\t<a href=\"single.php?cid=$cid\"><img src=\"thumbs/$file\" class=\"thumbnailImg\" width=\"150\" height=\"200\"/></a><br/>";
        echo "\n\t".$name. " ";
        echo "\n\t$". $price;
        echo "\n</div>";
        $results++; //Incrementing flag if results found.
        }//end while loop

    }
  }
...


}

Upvotes: 0

Guntram Blohm
Guntram Blohm

Reputation: 9819

Set a variable to 0 before the while() loop. Set it to 1 inside the loop. Print some text if it's still 0 after the loop. Like this:

$found=0;
while($row = mysql_fetch_array($result)){
    $found=1;
    ...
}//end while loop
if ($found==0) {
    echo "no results found";
}

Upvotes: 0

Set a flag counter and you will get it working.

$results=0; // Setting a flag here
while($row = mysql_fetch_array($result)){
        $name = $row['name'];
        $file = $row['file'];
        $description = $row['description'];
        $category = $row['category'];
        $brand = $row['brand'];
        $season = $row['season'];
        $price = $row['price'];
        $store = $row['store'];
        $cid = $row['cid'];

        echo "\n<div class=\"thumb\">";
        echo "\n\t<a href=\"single.php?cid=$cid\"><img src=\"thumbs/$file\" class=\"thumbnailImg\" width=\"150\" height=\"200\"/></a><br/>";
        echo "\n\t".$name. " ";
        echo "\n\t$". $price;
        echo "\n</div>";
        $results++; //Incrementing flag if results found.
        }//end while loop

    }
    else if($results==0) 
    {
    echo "<h2><em>No results were found.</em></h2>";
    }
    else{
        echo "<h2><em>No results were found.</em></h2>";
    }//end if search ,else

Upvotes: 1

Related Questions