简体   繁体   中英

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?

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";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM