dsfdfdfd6576578 2013-11-02 15:31
浏览 38
已采纳

使用Jquery自动完成图像

I am using the code bellow of foxycomplete to get some results from my db with images. With local files works great but i cant do it with mysql.

Here is my code that i am using..

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

The Javascript Code:

(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);

And the php code to retrieve my data:

<?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{

}
?>

Thank you Guys!!!

  • 写回答

1条回答 默认 最新

  • dongyang0005 2013-11-12 02:07
    关注

    Whats inside the $result['image']? If it contains the image file itself(binary data) and not just the path, then you need to create one more file (image.php?s=id) which would retreive just that binary data with adequate header.. ie

    <?php
    
    header('Content-Type: image/jpeg'); 
    
    // sql stuff 
    
    echo $result['image'];
    
    ?>
    

    In your main file you'll then call just

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

    also, dont torture yourself and use json_encode() :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法