php - 对图像使用 Jquery 自动完成

标签 php jquery

我正在使用 foxycomplete 下面的代码从我的数据库中获取一些带有图像的结果。 使用本地文件效果很好,但我不能使用 mysql。

这是我正在使用的代码..

<form id="autocompleteForm" name="autocompleteForm" action="" method="post">
    <input type="text" name="s" id="s" value="Search" style="border:none;" />
</form>

Javascript 代码:

(function($) {
    $(document).ready(function() {


        $( '#s' ).each( function(){
        $(this).attr( 'title', $(this).val() )
          .focus( function(){
            if ( $(this).val() == $(this).attr('title') ) {
              $(this).val( '' );
            }
          } ).blur( function(){
            if ( $(this).val() == '' || $(this).val() == ' ' ) {
              $(this).val( $(this).attr('title') );
            }
          } );
        } );

        $('input#s').result(function(event, data, formatted) {
            $('#result').html( !data ? "No match!" : "Selected: " + formatted);
        }).blur(function(){     
        });

        $(function() {      
        function format(mail) {
            return "<a href='"+mail.permalink+"'><img src='" + mail.image + "' /><span class='title'>" + mail.title +"</span></a>";         
        }

        function link(mail) {
            return mail.permalink
        }

        function title(mail) {
            return mail.title
        }

        $("#s").autocomplete(completeResults, {
            width: $("#s").outerWidth()-2,          
            max: 5,         
            scroll: false,
            dataType: "json",
            source: "video_search.php",
            matchContains: "word",
            parse: function(data) {
                return $.map(data, function(row) {
                    return {
                        data: row,
                        value: row.title,
                        result: $("#s").val()
                    }
                });
            },
            formatItem: function(item) {                
                return format(item);
            }
            }).result(function(e, item) {
                $("#s").val(title(item));
                //location.href = link(item);
            });                     
        });

    });
})(jQuery);

以及检索我的数据的 php 代码:

<?php
require "connectiondb.php";

if(isset($_REQUEST['s']))
{

$queryString = mysql_real_escape_string($_REQUEST['s']);

echo'var completeResults = [{';

$ss = mysql_query ("SELECT title, image FROM users WHERE title LIKE '$queryString%' LIMIT 7");
while($result = mysql_fetch_assoc($ss)) {   

echo '  
    "permalink": "index.html",
    "image": "'.$result['image'].'",
    "title": "'.$result['title'].'"
},';

}

echo '}];';


}else{

}
?>

谢谢你们!!!

最佳答案

$result['image'] 里面有什么?如果它包含图像文件本身(二进制数据)而不仅仅是路径,那么您需要再创建一个文件(image.php?s = id),它将只检索具有足够标题的二进制数据..即

<?php

header('Content-Type: image/jpeg'); 

// sql stuff 

echo $result['image'];

?>

在你的主文件中你将调用

"image": "'.image.php?id='.$result['id'].'",

另外,不要折磨自己并使用 json_encode() :)

关于php - 对图像使用 Jquery 自动完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19743518/

相关文章:

jquery - 禁用带有悬停意图的 # 链接的点击

javascript - 鼠标移出时淡出,除非在设定时间内鼠标悬停

javascript - Blade 中通过 Ajax 将变量发送到 Controller

php - 无法使用简单的 Web 应用程序进行部署/前进

php - 如何从 wordpress 中的 cli 启动 php 脚本

javascript - 当顶部横幅展开时让网站缩小

javascript - BigQuery - 基于字段/过滤器构建动态选择语句/查询

php - 自定义搜索引擎查询返回 false phpMyAdmin

javascript - 我怎样才能在全日历中显示比标题更多的内容

php - 登录页面无法使用有效凭据(PHP 和 MySQL)