在自动完成结果中显示图像

时间:2015-11-01 17:31:17

标签: php jquery

我将图片网址存储在我的数据库的B列中,希望在自动填充结果中以图片形式显示它们。我尝试按如下方式编写代码,但它只是以文本格式提供URL。我该怎么做才能解决它?非常感谢。

的index.php

<script type="text/javascript">
                $(document).ready(function(){
                    $("input[name='search']").autocomplete({
                        source:'search.php',
                        minLength:1
                    });
                });
        </script>

<input type="text" name="search" required/>

的search.php

$keyterms = $_GET['term'];
$query = $database->query("SELECT * FROM table WHERE A LIKE '%".$keyterms."%'    ORDER BY A ASC");
while ($row = $query->fetch_assoc()) {
    $data[] = $row['B'];}
echo json_encode($data);

3 个答案:

答案 0 :(得分:0)

这样的事情:

$keyterms = $_GET['term'];
$query = $database->query("SELECT * FROM table WHERE A LIKE '%".$keyterms."%'    ORDER BY A ASC");
while ($row = $query->fetch_assoc()) {
$data[] = '<a href="' . $row['B'] . '"></a>';}
echo json_encode($data);

注意:这不是最终解决方案,而是暗示您的解决方案。

答案 1 :(得分:0)

您需要为自动填充实现自定义视图,请在此处查看示例custom data and display autocomplete

答案 2 :(得分:0)

You can have a label and a value when using autocomplete

// Not sure about this part, never used Factory.StartNew before.
Task.Factory.StartNew(async delegate { await ReceiveData(_socket); }, TaskCreationOptions.LongRunning);

private async Task ReceiveData(SocketStream socket) {
    try {
        using (var reader = new DataReader(socket.InputStream)) {
            while (true) {
                uint received;
                do {
                    received = await reader.LoadAsync(4);
                    if (received == 0) break;
                } while (reader.UnconsumedBufferLength < 4);

                if (received == 0) break;

                var length = reader.ReadUInt32();
                do {
                    received = await reader.LoadAsync(length);
                    if (received == 0) break;
                } while (reader.UnconsumedBufferLength < length);

                if (received == 0) break;

                // Publish the data asynchronously using an event aggregator
                Console.WriteLine(reader.ReadString(length));
            }
        }
        // Client disconnected.
    }
    catch (IOException ex) {
        // Client probably disconnected. Can check hresult to be sure.
    }
    catch (Exception ex) {
        Console.WriteLine(ex);
    }
}

页面应该只处理它