私がやろうとしていること:
検索ボックスがあり、投稿、ユーザー名、またはユーザーの電子メールを検索できます。
投稿は 1 つのテーブルにあり、ユーザーは別のテーブルにあります。
構造は次のとおりです。
投稿
post_id user_id 投稿 post_date 可視
ユーザー
ユーザー 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;
}
}
/* 注 = 簡単にするために、コードはいくつかの場所で短縮されています */
両方のクエリが機能し、1 つずつ実行すると結果が返されます。つまり、最初のクエリと if ($post_query->num_rows() == 0) { の後のすべてのコードにコメントを付けると、コードの 2 番目の部分が正常に機能し、結果が返されます。反対側も同様です。
どんな助けでも深く感謝します。
よろしく、ゾラン
両方のコメントを外すと、エラーは発生せず、結果は 0 になります。
誰でも助けることができますか?