简体   繁体   English

使用PHP在MySQL表格中搜索自动完成框中的多个字段

[英]Searching a MySQL table, using PHP, for multiple fields in an auto complete box

Im trying to search two of the fields in my table. 我正在尝试搜索表格中的两个字段。 (Database table contains cocktail names, with their name, ingredients and description) When a user types their desired term into the search box, auto complete results are given using Ajax/jquery. (数据库表包含鸡尾酒名称,以及名称,成分和描述)。当用户在搜索框中键入所需的术语时,将使用Ajax / jquery给出自动完成的结果。

I've got it working searching one field (Name) but can't seem to get it to do more than the one(Name & Ingredients). 我已经在搜索一个字段(名称)上工作了,但是似乎并不能使它做更多的工作(名称和成分)。 Another problem is when the results are given (they are given as links) When you select them it puts the data in the entry box instead of taking you to the next page. 另一个问题是给出结果时(将它们作为链接给出)。选择结果时,它将数据放在输入框中,而不是转到下一页。

index.php: index.php:

<script>
$(document).ready(function(){
 $("#tag").autocomplete("autocomplete.php", {
        selectFirst: true
    });
});
</script>

    <label>Tag:</label>
    <input name="tag" type="text" id="tag" size="40"/>

autocomplete.php: autocomplete.php:

<?php
    $q=$_GET['q'];
    $my_data=mysql_real_escape_string($q);
    $mysqli=mysqli_connect('localhost','ignitet1','password','ignitet1_WhatCocktail') or die("Database Error");
    $sql="SELECT Name FROM tblCocktail WHERE Name LIKE '%$my_data%' ORDER BY Name";
    $result = mysqli_query($mysqli,$sql) or die(mysqli_error());

    if($result)
    {
        while($row=mysqli_fetch_array($result))
        {
            echo "<a href=\"details.php\">";
            echo $row['Name']."," , "<br />";
            echo "</a>";
        }
    }
?>

Any help would be appreciated. 任何帮助,将不胜感激。

I'm not sure about forcing a link to that page, but for a quick and dirty fix on the search I would add: OR Ingredients LIKE '%$my_dat%' before the ORDER clause. 我不确定是否要强制链接到该页面,但是为了在搜索时进行快速又肮脏的修复,我将添加: OR Ingredients LIKE '%$my_dat%' ORDER子句前的OR Ingredients LIKE '%$my_dat%'

Update: 更新:

user710502 is correct about the fields to be selected in the query. user710502关于要在查询中选择的字段是正确的。 As for the autocomplete, I looked at the documentation and it looks like you will have to create a custom event handler for the autocompleteselect event. 至于自动完成功能,我查看了文档 ,看起来您将必须为autocompleteselect事件创建一个自定义事件处理程序。 This event fires when the user chooses an option from the dropdown. 当用户从下拉菜单中选择一个选项时,将触发此事件。 The default behavior (as you know) is to fill the field with the 'value'. 默认行为(如您所知)是用“值”填充字段。 Your added code will look something like this: 您添加的代码将如下所示:

$("#selector-for-field").bind('autocompleteselect',function(event,ui){
    event.preventDefault(); // this will stop the field from filling
    // maybe make the value sent from the serverside a hyperlink,
    // eg. http://www.wherever.com/you/want/the/item/to/link-to
    // then in here do something like:
    window.location(ui.item.value);
});

You'll have to double check that ui.item.value works because I didn't check it. 您必须再次检查ui.item.value有效,因为我没有检查它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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