doujin8673 2013-02-18 12:58
浏览 27

使用ajax完成我的搜索输入

I know i've posted like 30 min ago but i think i am getting close with my search posts input:

i've created a model that looks like this

    function matchPosts($keyword)
{
    $this->db->get('posts');
    $data = array();
    $query = $this->db->query("SELECT title,body FROM posts WHERE title LIKE '%$keyword%' or body LIKE '$keyword%' AND status='published'");        
    if( $query->num_rows() > 0)
    {
        $data = $query->row_array();
    }       
    $query->free_result();
    return  $data;
}

i am trying to get a match from my database,

the controller looks like this:

public function searchPosts()
{
    $keyword = $this->input->post('search_value', TRUE);

    $matched_field = $this->Model_cats->matchPosts($keyword);

    echo $keyword;

    if( count($matched_field) > 0)
    {
        $this->load->view('posts_list');
    }
    else
    {
        $this->load->view('posts_list');
    }
}

this is the js file

$(document).ready(function()
{
    $("#search_posts").keyup(function()
    {        
        var searchValue = $(this).val();

        $(".posted_post").each(function()
        {
               $.ajax({             
                    type: "POST",
                    url : "http://local.blog.com/welcome/searchPosts",
                    data: {
                        search_value: searchValue   
                    },  
                    success: function(data)
                    { 
                        if(data)
                        {
                            $(this).show(); 
                        }
                        else
                        {
                            $(this).fadeOut();      
                        }

                    }
                });
        });
    });
});

and the view:

    <br>    
<form action="" method="post"> 
        <label for="search_posts"><b>Search Posts</b></label>
        <input type="text" id="search_posts" value="" />
</form>


posts_list is the view where i loop throught the posts...

Em i getting close? it doesn't work in this state.

  • 写回答

3条回答 默认 最新

  • dqhmtpuy94946 2013-02-18 13:14
    关注

    Edit your script as

    $("#search_posts").keyup(function()
    {        
        var posts = $(this).val();        
        //$(".posted_post").each(function()
        //{
            $.ajax({
                url : 'http://local.blog.com/welcome/searchPosts',
                data: {posts},  
                success : function(data)
                {
                     $(this).show();
                }
            });
        //}
    });
    

    and from

    $matched_field = $this->Model_cats->matchPosts($keyword);
    

    the $matched_field returns number of search results found right..??then you just check like

    if($matched_field)
    {
          //Here you can print result of search
    }
    else
    {
          //You can print No results found
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致