如何使用CodeIgniter在Ajax / JSON实时搜索中添加链接

时间:2016-06-13 07:22:48

标签: php jquery json ajax codeigniter

我正在使用我在某篇文章中看到的这种搜索方法,它运行正常,它显示如下:

when link is not added

但在添加显示该实时搜索链接的代码后,它会显示:

when link code is added

我想要的是显示一个看起来像第一张图片的链接。 这是我的ajax代码:

  <script>
    var base_url = "<?=base_url()?>";
    $(document).ready(function(){
        $("#search").keyup(function(){
            if($("#search").val().length>1){
                $.ajax({
                    type: "post",
                    url :  base_url + "Isidran/searchFirm",
                    cache: false,
                    data:'search='+$("#search").val(),
                    success: function(response){
                        $('#finalResult').html("");
                        var obj = JSON.parse(response);
                        if(obj.length>0){
                            try{
                                var items=[];
                                $.each(obj, function(i,val){
                                    var href = '<?php echo site_url('Profile/view'); ?>'+ "/" + val.userID + "/" + val.username;
                                    items.push($("<a href='"+href+"'>").text(val.firstName + " " + val.lastName + " (" + val.username + ")"));
                                });
                                $('#finalResult').append.apply($('#finalResult'), items);
                            }catch(e) {
                                alert('error on request..');
                            }
                        }else{
                            $('#finalResult').html($('<p>').text("No Data Found"));
                        }

                    },
                    error: function(){
                        alert('Error while request..');
                    }
                });
            }
            return false;
        });
    });
    </script>

我的控制员:

public function searchFirm(){

        $search =  $this->input->post('search');
        $query = $this->Blog_model->getFirm($search);
        echo json_encode ($query);
    }

我的模特:

//sorry for this query, but it works fine any suggestion to make this better 
//will be very helpful
public function getFirm($search){

$query = $this->db->query("SELECT * FROM `blog`.`tbl_users`, `blog`.`tbl_userinfo`, `blog`.`tbl_uploads` 
WHERE (`tbl_users`.`userID`=`tbl_userinfo`.`user_ID` AND `tbl_users`.`userID`=`tbl_uploads`.`user_ID`  AND `tbl_users`.`username` like '%$search%')
OR (`tbl_users`.`userID`=`tbl_userinfo`.`user_ID` AND `tbl_users`.`userID`=`tbl_uploads`.`user_ID` AND `tbl_userinfo`.`firstName` like '%$search%') 
OR (`tbl_users`.`userID`=`tbl_userinfo`.`user_ID` AND `tbl_users`.`userID`=`tbl_uploads`.`user_ID`  AND `tbl_userinfo`.`lastName` like '%$search%')");

return $query->result();

}

提前感谢。

0 个答案:

没有答案