回答編集履歴

1

追記

2018/02/22 08:31

投稿

yambejp
yambejp

スコア114837

test CHANGED
@@ -5,3 +5,123 @@
5
5
  所属グループテーブルをつくり、団体ごとにidを振り
6
6
 
7
7
  ユーザー=所属連携テーブルを別途つくって紐付けます
8
+
9
+
10
+
11
+ # sample
12
+
13
+
14
+
15
+ とりあえず今のままのデータ構成で検索するならこう
16
+
17
+ ```PHP
18
+
19
+ $freeword=filter_input(INPUT_POST,'freeword');
20
+
21
+ $sex=filter_input(INPUT_POST,'sex',FILTER_VALIDATE_REGEXP,["options"=>["regexp"=>"/^(男|女)$/u","default"=>NULL]]);
22
+
23
+ $syozoku=filter_input(INPUT_POST,'syozoku',FILTER_VALIDATE_REGEXP,["options"=>["regexp"=>"/^(運動部|文化部|帰宅部|生徒会)$/u","default"=>NULL]]);
24
+
25
+
26
+
27
+ $sql = "SELECT * FROM students WHERE 1 ";
28
+
29
+ $data=[];
30
+
31
+
32
+
33
+ if(!is_null($freeword) and $freeword!==""){
34
+
35
+ $sql.= " and (0 ";
36
+
37
+ $sql.= " or student_number LIKE ?";
38
+
39
+ $sql.= " or student_name LIKE ?";
40
+
41
+ $sql.= " or email LIKE ?";
42
+
43
+ $sql.= " ) ";
44
+
45
+ $data=array_merge($data,array_fill(0,3,'%'.$freeword.'%'));
46
+
47
+ }
48
+
49
+
50
+
51
+ if(!is_null($sex)){
52
+
53
+ $sql.= " and sex=?";
54
+
55
+ $data[]=$sex;
56
+
57
+ }
58
+
59
+ if(!is_null($syozoku)){
60
+
61
+ $sql.= " and syozoku regexp ?";
62
+
63
+ $data[]="(^|,)".$syozoku."(,|$)";
64
+
65
+ }
66
+
67
+ print $sql."<br>";
68
+
69
+ print_r($data);
70
+
71
+
72
+
73
+ /*
74
+
75
+ 検索結果の表示
76
+
77
+ $stmt = $pdo->prepare($sql);
78
+
79
+ $stmt->execute($data);
80
+
81
+ $rows=$stmt->fetchAll(PDO::FETCH_ASSOC);
82
+
83
+ print_r($rows);
84
+
85
+ */
86
+
87
+ ?>
88
+
89
+ <form method="post">
90
+
91
+ 条件を絞り込む<br>
92
+
93
+ フリーワード<input type="text" name="freeword"><br>
94
+
95
+ 性別
96
+
97
+ <select name="sex">
98
+
99
+ <option value="">---</option>
100
+
101
+ <option value="男">男</option>
102
+
103
+ <option value="女">女</option>
104
+
105
+ </select><br>
106
+
107
+ 所属
108
+
109
+ <select name="syozoku">
110
+
111
+ <option value="">------</option>
112
+
113
+ <option value="運動部">運動部</option>
114
+
115
+ <option value="文化部">文化部</option>
116
+
117
+ <option value="帰宅部">帰宅部</option>
118
+
119
+ <option value="生徒会">生徒会</option>
120
+
121
+ </select><br>
122
+
123
+ <input type="submit" value="検索する"></input>
124
+
125
+ </form>
126
+
127
+ ```