gpt4 book ai didi

php - 使用ajax完成我的搜索输入

转载 作者:行者123 更新时间:2023-12-02 18:59:34 25 4
gpt4 key购买 nike

我知道我大约 30 分钟前就发布了帖子,但我想我已经接近搜索帖子输入了:

我创建了一个如下所示的模型

    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;
}

我正在尝试从我的数据库中获取匹配项,

Controller 看起来像这样:

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');
}
}

这是js文件

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

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

和 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 是我循环浏览帖子的 View ...

我已经接近了吗?在这种状态下它不起作用。

最佳答案

在您的 match_posts 函数中,您使用的是 num_rows() 在您的搜索帖子函数中,您正在收集上述函数的结果,并使用 === 与 bool true 进行比较。尝试 var dump 来确认从 num rows 函数返回什么类型的值。我认为您正在比较 numeric 和 boolean 。相反,您可以使用 if count (matched fields) > 0

关于php - 使用ajax完成我的搜索输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14936944/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com