我想做什么:
我有一个搜索框,可以搜索帖子或用户名或用户电子邮件。
帖子在一个表中,用户在另一个表中。
这是结构:
帖子
POST_ID 用户身份 岗位 发布日期 可见
用户
用户标识 用户名 电子邮件 名字 名字
现在,我在我的模型中尝试以下代码:
<?php
public function posts_that_match_search_term($search_term) {
$post_query = $this->db->query("SELECT * FROM posts,users WHERE post_message LIKE '%$search_term%' AND post_user_id=user_id ORDER BY post_id DESC", array());
$post_query2 = $this->db->query("SELECT * FROM users WHERE user_username LIKE '%$search_term%' OR user_email LIKE '%$search_term%' ORDER BY user_id DESC", array());
if ($post_query->num_rows() == 0) {
return array();
} else {
$i = 0;
foreach ($post_query->result() AS $row) {
$post_array[$i]["post_id"] = $row->post_id;
$post_array[$i]["post_message"] = $row->post_message;
$post_array[$i]["post_datetime"] = $row->post_datetime;
$post_array[$i]["post_completion_status"] = $row->post_completion_status;
$post_array[$i]["post_completion_date"] = $row->post_completion_date;
$post_array[$i]["post_completion_notes"] = $row->post_completion_notes;
}
return $post_array;
}
if ($post_query2->num_rows() == 0) {
return array();
} else {
$a = 0;
foreach ($post_query2->result() AS $row2) {
$post_array2[$a]["user_id"] = $row2->user_id;
$post_array2[$a]["user_name"] = $row2->user_name;
$post_array2[$a]["user_username"] = $row2->user_username;
$post_array2[$a]["user_image_filename"] = $row2->user_image_filename;
$post_array2[$a]["user_first_name"] = $row2->user_first_name;
$post_array2[$a]["user_last_name"] = $row2->user_last_name;
$post_array2[$a]["user_email"] = $row2->user_email;
$a++;
}
return $post_array2;
}
}
/ * note =为了简单起见,在某些地方缩短了代码* /
如果我逐个运行查询,则两个查询都可以工作并返回结果。那就是如果我评论第一个查询和if之后的所有代码($ post_query-&gt; num_rows()== 0){那么代码的第二部分正常工作并返回结果。对于相反的情况也是如此。
任何帮助都将深深体会到。
关心,佐兰
当两者都被取消注释时,我没有错误,只有0结果。
任何人都可以提供帮助吗?
答案 0 :(得分:0)
卓然, 您必须创建一个检查用户名或电子邮件的查询。你知道怎么做这样的连接:
$this->db->join('posts', posts.userid = users.userid');
所以你加入这两个表,然后搜索他们的用户名或电子邮件。让我知道你提出了什么!