Response bot using keywords

193 views Asked by At

I made a q&a bot using PHP codes. It works in a way where the user has to key in questions in the 'Search' bar provided. After clicking on the 'Submit' button, the program will search for specific keywords and reply to the question asked. However, how do I show just one reply if there are more than one keywords in the question asked? Also, how do I display an error message when the user does not ask a question without the specific keywords?

if (isset($_GET['searchterm']))
{
$question = $_GET['searchterm'];
echo "<b>$question</b>";
echo "<br />";

$token = strtok($question, " ");

while($token !== false)
{
$token = strtok(" ");

switch ($token) {
case "hot":
    print "It is now ".$data['main']['temp']. " °C"." hot"."<br>";
    break;
case "cold":
    print "It is ".$data['main']['temp']. " °C"."<br>";
    break;
 case "warm":
    print "It is ".$data['main']['temp']. " °C"." warm"."<br>";
    break;
case "cool":
    print "The temperature is ".$data['main']['temp']. " °C"." cool"."<br>";
    break;
case "temperature":
    print "The temperature now is ".$data['main']['temp']. " °C"."<br>";
    break;
case "current":
    print "The current temperature is ".$data['main']['temp']. " °C"."<br>";
    break;
case "weather":
    print "We are now having ".$data['weather'][0]['main']."<br>";
    break;  
case "wind":
    print "The wind speed is currently ".$data['wind']['speed']. " m/s"."<br>";
    break;      
}
}   
}
else {
    print "Sorry, we did not understand your question.";
}
1

There are 1 answers

1
GThamizh On
Use default conditon on your code:
switch ($i) {
    switch ($token) 
        {
            case "hot":
                print "It is now ".$data['main']['temp']. " °C"." hot"."<br>";
                break;
            case "cold":
                print "It is ".$data['main']['temp']. " °C"."<br>";
                break;
             case "warm":
                print "It is ".$data['main']['temp']. " °C"." warm"."<br>";
                break;
            case "cool":
                print "The temperature is ".$data['main']['temp']. " °C"." cool"."<br>";
                break;
            case "temperature":
                print "The temperature now is ".$data['main']['temp']. " °C"."<br>";
                break;
            case "current":
                print "The current temperature is ".$data['main']['temp']. " °C"."<br>";
                break;
            case "weather":
                print "We are now having ".$data['weather'][0]['main']."<br>";
                break;  
            case "wind":
                print "The wind speed is currently ".$data['wind']['speed']. " m/s"."<br>";
                break;
            default:
            print "wrong question";
        }
}