DavidDuffy95
DavidDuffy95

Reputation: 21

How to add a "No results found" message using PHP?

I am currently building a website that relies on pulling information from my localhost database through a search bar. I currently have it displaying all the information correctly, but when a search doesn't match anything in my database then it just shows nothing, how do I change this to "No results found" or something of the sort? My code is:

<?php

    require_once 'connect.php';

    if(isset($_GET['keywords'])){

        $keywords = $db->escape_string($_GET['keywords']);  

        $query = $db->query("
                        SELECT movie, game
                        FROM movies
                        WHERE movie LIKE '%{$keywords}%'
                     ");
?>

<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Example</title>
    <link rel="stylesheet" type="text/css" href="styles.css">
</head>

<body>
    <h1><a href="index.php">Test</a></h1>
    <div class="search">
        <form action="search.php" method="get">
            <input name="keywords" type="text" placeholder="Type something!" autocomplete="off" size="40">
            <input type="submit" value="Search!">
        </form>
    </div>
<br />
<?php

    if($query->num_rows){
        while($r = $query->fetch_object()){
        ?>
    <div class="movie"> <a><?php echo $r->movie; ?></a> </div><br />
    <div class="result"> <a><?php echo $r->game; ?></a> </div><br />
</body>
</html>
<?php
    }
}

}

Sorry for pasting literally the full page, but I don't even know where to start as I'm a PHP noob.

Upvotes: 2

Views: 808

Answers (3)

Nana Partykar
Nana Partykar

Reputation: 10548

<?php

require_once 'connect.php';

if(isset($_GET['keywords']))
{
    $keywords = $db->escape_string($_GET['keywords']);  
    $query = $db->query("SELECT movie, game FROM movies WHERE movie LIKE '%{$keywords}%'");
    ?>
    <!doctype html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Example</title>
    <link rel="stylesheet" type="text/css" href="styles.css">
    </head>
    <body>
    <h1><a href="index.php">Test</a></h1>
    <div class="search">
        <form action="search.php" method="get">
          <input name="keywords" type="text" placeholder="Type something!" autocomplete="off" size="40">
          <input type="submit" value="Search!">
        </form>
    </div>
    <br />
    <?php
    if($query->num_rows)
    {
        while($r = $query->fetch_object())
        {
        ?>
        <div class="movie"> <a><?php echo $r->movie; ?></a> </div><br />
        <div class="result"> <a><?php echo $r->game; ?></a> </div><br />
        <?}
    }
    else {?>
     <div class="noResult">No Results Found</div>
    <?}?>
    </body>
    </html>
<?php }?>

styles.css (Here, using this class name, whatever property you want you can set. I gave few. Example.)

.noResult{
color: #800000;
font-size:14 px;
}

Upvotes: 0

Farhan
Farhan

Reputation: 1483

if($query->num_rows){..} condition will be true if query result have more then 0 results, so just use else part to print message that no result(s) found

<?php

    require_once 'connect.php';

    if(isset($_GET['keywords'])){

    $keywords = $db->escape_string($_GET['keywords']);  

    $query = $db->query("
    SELECT movie, game
    FROM movies
    WHERE movie LIKE '%{$keywords}%'
    ");
    ?>

    <!doctype html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Example</title>
    <link rel="stylesheet" type="text/css" href="styles.css">
    </head>

    <body>
    <h1><a href="index.php">Test</a></h1>
    <div class="search">
      <form action="search.php" method="get">
        <input name="keywords" type="text" placeholder="Type something!" autocomplete="off" size="40">
        <input type="submit" value="Search!">
      </form>
    </div>
    <br />
    <?php

    if($query->num_rows){
        while($r = $query->fetch_object()){
            ?>
    <div class="movie"> <a><?php echo $r->movie; ?></a> </div><br />
    <div class="result"> <a><?php echo $r->game; ?></a> </div><br />
    </body>
    </html>
    <?php
        }
    }
    else{
        echo "No Result Found!";
        }

    }

Upvotes: 4

Santy
Santy

Reputation: 387

to fit with your opening and closing tags you can use:

if($query->num_rows){
    while($r = $query->fetch_object()){
    ?>
<div class="movie"> <a><?php echo $r->movie; ?></a> </div><br />
<div class="result"> <a><?php echo $r->game; ?></a> </div><br />

<?php
    } 
} else {
    ?>
        <div>No results</div>
    <?php
}
?>
</body>
</html>

do keep the /body and /html tags after any output.

Upvotes: 0

Related Questions