プロが教える店舗&オフィスのセキュリティ対策術

<form id="form1" name="form1" method="post" action="lab.php">
<label for="textfield">しょうゆ検索エンジン<br />
</label>
<input type="text" name="lab" value=""/>
<input type="submit" name="button" id="button" value="検索" />
</form><br>
<form id="form1" name="form1" method="post" action="lab.php">
<label for="textfield">成分エンジン<br />
</label>
<input type="text" name="lab" value="" />
<input type="submit" name="button" id="button" value="検索" />
</form>

このようなフォーム内に、例えば醤油検索エンジンに「キッコーマン 山さ」と入力したらsyouカラムから該当するデータを取り出し表示するにはどうしたら良いでしょうか?
フォームに「キッコーマン 山さ ヤマキ」
と入力したら
※3件あります
醤油:キッコーマン
成分:大豆、油

醤油:山さ
成分:大豆、塩

醤油:ヤマキ
成分:醤油

と表示したいです。簡単だろうと考えては見た物の、残念ながら解決に至らず質問した次第であります。よろしく願います。
あと、莫大な量を登録したいのでnum_rowsなどで
syounum=array("キッコーマン","山さ","ヤマキ")
等とphpに記述するのは避けたいです。よろしく願います。

A 回答 (3件)

#2です



>「味噌」と検索されたら味噌の該当データを
>「グルタミン」と検索されたらグルタミンの該当データを取り出せるPHPにしたいのです。

そういうソースを提示していますが、SQLは使わないという意味ですか?
ごめん、いつもながらなにがいいたいかわからないや
    • good
    • 0
この回答へのお礼

とりあえず参考書で解決しました。ありがとうございました。

お礼日時:2011/10/15 10:57

いつも疑問におもうのだが本当に覚える気がある?


まずPHPからSQLにデータを渡すやり方はおいといて
SQLとしてどうやって絞り込みをするかから勉強しなさい
PHPとの絡みはそれからだ。

○データの持ち方
//メーカーテーブル
create table t_maker(id int not null primary key,name varchar(30));
insert into t_maker values(1,'キッコーマン'),(2,'山さ'),(3,'ヤマキ'),(4,'ヒゲタ醤油'),(5,'ヒガシマル醤油');

//成分テーブル
create table t_seibun(id int not null primary key,name varchar(30));
insert into t_seibun values(1,'大豆'),(2,'塩'),(3,'油'),(4,'酒'),(5,'魚粉');

//メーカー・成分テーブル
create table t_maker_seibun(maker_id int,seibun_id int,unique key (maker_id ,seibun_id ));
insert into t_maker_seibun values(1,1),(1,2),(2,1),(2,3),(3,1),(3,2),(3,4),(4,1),(4,2),(4,3),(5,4),(5,5);

○結果
//とりあえず全通り組合せ
select maker_id,m.name as maker_name,group_concat(s.name order by s.id) as seibun_names
from t_maker_seibun as ms
inner join t_maker as m on ms.maker_id=m.id
inner join t_seibun as s on ms.seibun_id=s.id
group by maker_id;

//メーカーが「山さ」か「ヤマキ」
select maker_id,m.name as maker_name,group_concat(s.name order by s.id) as seibun_names
from t_maker_seibun as ms
inner join t_maker as m on ms.maker_id=m.id and m.name in('山さ','ヤマキ')
inner join t_seibun as s on ms.seibun_id=s.id
group by maker_id;

//成分に「塩」か「油」の少なくともどちらかが入っている
select maker_id,m.name as maker_name,group_concat(s.name order by s.id) as seibun_names
from t_maker_seibun as ms
inner join t_maker as m on ms.maker_id=m.id
inner join t_seibun as s on ms.seibun_id=s.id
where maker_id in (
select maker_id from t_seibun
inner join t_maker_seibun on seibun_id=id
and name in('塩','油')
)
group by maker_id

//成分に「大豆」がはいっていない
select maker_id,m.name as maker_name,group_concat(s.name order by s.id) as seibun_names
from t_maker_seibun as ms
inner join t_maker as m on ms.maker_id=m.id
inner join t_seibun as s on ms.seibun_id=s.id
where not maker_id in (
select maker_id from t_seibun
inner join t_maker_seibun on seibun_id=id
and name in('大豆')
group by maker_id
)
group by maker_id

//成分に「大豆」かつ「油」の両方が入っている
select maker_id,m.name as maker_name,group_concat(s.name order by s.id) as seibun_names
from t_maker_seibun as ms
inner join t_maker as m on ms.maker_id=m.id
inner join t_seibun as s on ms.seibun_id=s.id
where maker_id in (
select maker_id from t_seibun
inner join t_maker_seibun on seibun_id=id
and name in('大豆','油')
group by maker_id
having count(maker_id) =2
)
group by maker_id

この回答への補足

莫大な量をDBに登録します。油が入っているとか、PHPに記入せずに、「味噌」と検索されたら味噌の該当データを「グルタミン」と検索されたらグルタミンの該当データを取り出せるPHPにしたいのです。

補足日時:2011/10/14 19:08
    • good
    • 0

>莫大な量を登録したいので



データベースを使うべきでは?
DBならLIKE検索で簡単に実装できると思いますよ。

この回答への補足

簡単ですか?
ポスト(name="syou")で送信
phpで
$syou=$_POST['syou']
で受信
select * from kensaku where '%$syou%'
で検索したら良いですか?

補足日時:2011/10/14 17:42
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!