Live搜索的使用codeigniter的Mysql [英] Live search using Codeigniter Mysql

查看:119
本文介绍了Live搜索的使用codeigniter的Mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做的Live搜索一个项目使用jQuery阿贾克斯,PHP codeigniter和MySQL

I am doing a project on Live Search Using Jquery Ajax, Php Codeigniter and Mysql

下面是我的模型code:

Here is my model Code:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Subjectmodel extends CI_Model {
   public function __construct() {
        parent::__construct();

    }
    function getSubject($search){
        $this->db->select("SUB_ID,NAME");
        $whereCondition = array('NAME' =>$search);
        $this->db->where($whereCondition);
        $this->db->from('ix08s_subjects');
        $query = $this->db->get();
        return $query->result();
    }
}
?>

下面是我的控制器code:

Here is my controller code:

<?php
class Subject extends CI_Controller {
    function __construct() {
        parent::__construct();
        $this->load->model('SubjectModel');
    }
    public function index(){
        $search=  $this->input->post('search');
        $query = $this->SubjectModel->getSubject($search);
        echo json_encode ($query);
    }
}
?>

这是我的看法code:

and this is my view code :

<html>
<head>
<script type="text/javascript" language="javascript" src="http://somexyz/js/javascripts/plugin/jquery.js"></script>
<script type="text/javascript" src="http://somexyz/js/javascripts/plugin/json2.js"></script>
<script>
    $(document).ready(function(){
      $("#search").keyup(function(){
        if($("#search").val().length>3){
        $.ajax({
            type: "post",
            url: "http://localhost/ibps/index.php/subject",
            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){                                            
                            items.push($('<li/>').text(val.NAME));
                        }); 
                        $('#finalResult').append.apply($('#finalResult'), items);
                    }catch(e) {     
                        alert('Exception while request..');
                    }       
                }else{
                    $('#finalResult').html($('<li/>').text("No Data Found"));       
                }       

            },
            error: function(){                      
                alert('Error while request..');
            }
        });
        }
        return false;
      });
    });
</script>
</head>
<body>
<div id="container">
<input type="text" name="search" id="search" />
<ul id="finalResult"></ul>
</div>
</body>
</html>

这是我的输出:

[{SUB_ID:1,名称:物理},{SUB_ID:2,名称:物理}]

[{"SUB_ID":"1","NAME":"physics"},{"SUB_ID":"2","NAME":"physics"}]

不过,这应该是正确的输出

But This should be the correct output

http://jsfiddle.net/serigo1990/uShzQ/

得到的答案

推荐答案

添加数据类型:JSON阿贾克斯设置

这篇关于Live搜索的使用codeigniter的Mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆