今回、サイト内「鈴木 スポーツ」検索ワードで検索すると検索データが出るようなプログラムを作りたいです。
DBから参照すること。
テーブル'topic','user','grop'3つテーブルがあります。
それぞれ、nameとtagフィールドがあります。
この3つテーブル中nameとtagフィールドからキーワードを参照する
現状、下記のようなコードを書きましたが、検索結果0件です。
if($_POST["search"]): $_SESSION["res"] = array(); $where = " WHERE 1 AND"; $kensaku = htmlspecialchars($_POST['search']); $kensaku = trim($kensaku); $word = str_replace(" ", " ", $kensaku); $word = str_replace("'", "\'", $word); $words = preg_split("/[ ]+/",$word); $count = count($words); else: if($_GET["tag"]): $_SESSION["res"] = array(); $where = " WHERE 1 AND"; $kensaku = htmlspecialchars($_GET["tag"]); $kensaku = trim($kensaku); $word = str_replace(" ", " ", $kensaku); $word = str_replace("'", "\'", $word); $words = preg_split("/[ ]+/",$word); $count = count($words); else: $err = "検索タグワードが入力されていません"; endif; endif; //topicから検索 $db->tbl_name = "topic"; if($count == 1): //入力文字が1つの場合 $img_topic = $db->Asearch("tag" , $words[0]); else: //入力文字が複数あった場合 $where .= " `tag` LIKE '%" . $words[0] . "%'"; if($count > 1): for($i = 1; $i < $count; $i++): $where .= " AND `tag` LIKE '%" . $words[$i] . "%'"; endfor; endif; $table = "topic"; $sql="SELECT * FROM `{$table}` {$where}"; $topic = $db->get_query($sql); endif; //userから検索 $db->tbl_name = "user"; if($count == 1): //入力文字が1つの場合 $img_topic = $db->Asearch("tag" , $words[0]); else: //入力文字が複数あった場合 $where .= " `name` LIKE '%" . $words[0] . "%'"; if($count > 1): for($i = 1; $i < $count; $i++): $where .= " AND `name` LIKE '%" . $words[$i] . "%'"; endfor; endif; $table = "user"; $sql="SELECT * FROM `{$table}` {$where}"; $user= $db->get_query($sql); endif; //gropから検索 $db->tbl_name = "grop"; if($count == 1): //入力文字が1つの場合 $img_topic = $db->Asearch("tag" , $words[0]); else: //入力文字が複数あった場合 $where .= " `tag` LIKE '%" . $words[0] . "%'"; if($count > 1): for($i = 1; $i < $count; $i++): $where .= " AND `tag` LIKE '%" . $words[$i] . "%'"; endfor; endif; $table = "grop"; $sql="SELECT * FROM `{$table}` {$where}"; $grop= $db->get_query($sql); endif; ・ ・ ・
回答4件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/07/11 09:40