Program-O Chatbot自动完成

时间:2017-09-28 16:25:16

标签: php jquery mysql chatbot program-o

我正在Program-o chatbot之上进行开发,并且在大多数情况下,它工作得很好。

  1. 我正试图让#34;说"输入字段自动完成基于机器人已经从AIML定义知道的内容。
  2. 我的问题是我不知道他们的逻辑/ SQL如何确定返回的内容,以便我为自动完成功能构建SQL。
  3. 我正在使用聊天机器人的JSON / jQuery版本,并且没有对代码进行核心更改。添加了Jquery和jQuery UI CDN库(用于自动完成JS)。自动填充代码来自David Carr
  4. 感谢您提供任何帮助!

    //I added autocomplete JS within index.php
    
        $(function() {
    
            //autocomplete
            $(".auto").autocomplete({
                source: "search.php",
                minLength: 1
            });                
    
        });
    
    //Search.php included within index.php
    
    <?php
    define('DB_SERVER', 'localhost');
    define('DB_USER', 'root');
    define('DB_PASSWORD', 'root');
    define('DB_NAME', 'demo');
    
    if (isset($_GET['say'])){
        $return_arr = array();
    
        try {
            $conn = new PDO("mysql:host=".DB_SERVER.";port=8889;dbname=".DB_NAME, DB_USER, DB_PASSWORD);
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
            $stmt = $conn->prepare('SELECT something FROM someTable WHERE something LIKE :say');
            $stmt->execute(array('term' => '%'.$_GET['term'].'%'));
    
            while($row = $stmt->fetch()) {
                $return_arr[] =  $row['something'];
            }
    
        } catch(PDOException $e) {
            echo 'ERROR: ' . $e->getMessage();
        }
    
        /* Toss back results as json encoded array. */
        echo json_encode($return_arr);
    }
    
    ?>
    

1 个答案:

答案 0 :(得分:0)

我没有正确处理这个问题。您可以执行如下查询。确保包含jQuery UI CSS和JS文件以实现此目的。最后,下面的脚本不显示表单本身,但确保您输入id =“say”以使其正常工作。更好的答案是值得赞赏的,但这是我能够提出的。谢谢

//Main File / index.php etc
<script>
$(function() {
    $( "#say" ).autocomplete({
       source: "search.php",
       minLength: 1
    });
});
</script>


//search.php
<?php
$host="localhost";
$username="uid";
$password="pwd";
$dbname="name";

//create a connection with dbname
$conn=mysqli_connect($host,$username,$password,$dbname);
if(!$conn)
{
 die("error in establishing connection: ". mysqli_connect_error());
}

$search=$_GET['term'];

//select query to get data from table
$sql="SELECT pattern from aiml WHERE pattern LIKE '%".$search."%'";

//run the above query
$result=mysqli_query($conn,$sql);

//display all the records
while($row=mysqli_fetch_assoc($result))
{
 //storing all the values of 'post_title' field one by one in an array
 $data[]=$row['pattern'];
}

//return json data
echo json_encode($data);
?>