Andrew
Andrew

Reputation: 243

Else statement doesn't work in option select

I am trying to implement a dropdown search option. All my search results are working. All the commands that I have assigned to if statements work, but when it does to else it deosn't work.

Here is my code:

if(isset($_REQUEST['submit'])){

    $opt = $_POST['opt'];

    if($opt==1){//if opt = 1
        $sqle = "SELECT * FROM tbl_events WHERE title LIKE '%{$keywords}%'";
        $resulte = mysql_query($sqle,$con) or die(mysql_error());

        while($row=mysql_fetch_array($resulte)){
            echo "<h4>" . $row['title'] . "</h4><br/>";
            echo "<p>" . $row['description'] . "<p>";

        }
    }else if($opt==2){//if opt = 2
        $sqls = "SELECT * FROM tbl_games WHERE games_name LIKE '%{$keywords}%'";
        $results = mysql_query($sqls,$con)or die(mysql_error());

        while($row=mysql_fetch_array($results)){
            echo "<h4>" . $row['games_name'] . "</h4><br/>";
            echo "<p>" . $row['description'] . "<p>";
        }
    }else{

        echo "Your Searched keyword did not match";
    }           
}

What to do?

Upvotes: 1

Views: 82

Answers (1)

Ruprit
Ruprit

Reputation: 743

Try this: Take a flag to check if record exists.

$flag = false;

if($opt==1){//if opt = 1
    $sqle = "SELECT * FROM tbl_events WHERE title LIKE '%{$keywords}%'";
    $resulte = mysql_query($sqle,$con) or die(mysql_error());

    if(mysql_num_rows($resulte) > 0) {

      $flag = true;
      while($row=mysql_fetch_array($resulte)){
      echo "<h4>" . $row['title'] . "</h4><br/>";
      echo "<p>" . $row['description'] . "<p>";

      }

    }

}else if($opt==2){//if opt = 2
    $sqls = "SELECT * FROM tbl_games WHERE games_name LIKE '%{$keywords}%'";
    $results = mysql_query($sqls,$con)or die(mysql_error());
    if(mysql_num_rows($resulte) > 0) {
      $flag = true;
      while($row=mysql_fetch_array($results)){
      echo "<h4>" . $row['games_name'] . "</h4><br/>";
      echo "<p>" . $row['description'] . "<p>";
      }
    }
}


if(!$flag){
    echo "Your Searched keyword did not match";
}

Upvotes: 1

Related Questions