回答編集履歴

6

調整

2018/10/23 05:07

投稿

yambejp
yambejp

スコア114883

test CHANGED
@@ -115,3 +115,95 @@
115
115
  }
116
116
 
117
117
  ```
118
+
119
+
120
+
121
+ # idをpostで渡す
122
+
123
+ ```PHP
124
+
125
+ <?PHP
126
+
127
+ $rows=[];
128
+
129
+ $id=filter_input(INPUT_POST,"id");
130
+
131
+ $data=[];
132
+
133
+ $product = ['製品A','製品B','製品C','製品D','製品E'];
134
+
135
+ try{
136
+
137
+ $sql = "SELECT productarry FROM tbl where 1 ";
138
+
139
+ if(!is_null($id) and $id!==false){
140
+
141
+ $sql.= "and id=? ";
142
+
143
+ array_push($data,$id);
144
+
145
+ }
146
+
147
+ $sql.= "and productarry in('".implode("','",$product)."') ";
148
+
149
+ print $sql;//debug
150
+
151
+ print_r($data);
152
+
153
+ //$stm = $pdo->prepare($sql);
154
+
155
+ //$stm->execute($data);
156
+
157
+ //$rows = $stm->fetchAll(PDO::FETCH_ASSOC);
158
+
159
+ }catch(PDOException $e){
160
+
161
+ die($e->getMessage());
162
+
163
+ }
164
+
165
+ $data=array_map(function($x){return $x["productarry"];},$rows);
166
+
167
+ echo <<<eof
168
+
169
+ <div class='form-group'>
170
+
171
+ <div class='col-lg-8'>
172
+
173
+ <div class='checkbox' id='product'>
174
+
175
+ <h4>Product Type</h4>
176
+
177
+ eof;
178
+
179
+ foreach ($product as $val) {
180
+
181
+ $checked=(in_array($val,$data)!==false)?" checked":"";
182
+
183
+ echo "<label><input type='checkbox' name='productarry[]' value='{$val}'{$checked}>{$val}</label><span style='margin-right: 25px;'></span>\n";
184
+
185
+ }
186
+
187
+ ?>
188
+
189
+ <hr>
190
+
191
+ <form method="post">
192
+
193
+ id test:<select name="id">
194
+
195
+ <option value="1">1</option>
196
+
197
+ <option value="2">2</option>
198
+
199
+ <option value="3">3</option>
200
+
201
+ </select>
202
+
203
+ <input type="submit" value="go">
204
+
205
+ </form>
206
+
207
+
208
+
209
+ ```

5

id

2018/10/23 05:06

投稿

yambejp
yambejp

スコア114883

test CHANGED
File without changes

4

id

2018/10/23 04:09

投稿

yambejp
yambejp

スコア114883

test CHANGED
@@ -64,6 +64,10 @@
64
64
 
65
65
  $rows=[];
66
66
 
67
+ $id=2;
68
+
69
+
70
+
67
71
  $product = ['製品A','製品B','製品C','製品D','製品E'];
68
72
 
69
73
  try{
@@ -78,7 +82,7 @@
78
82
 
79
83
  $stm = $pdo->prepare($sql);
80
84
 
81
- $stm->execute([2]);
85
+ $stm->execute([$id]);
82
86
 
83
87
  $rows = $stm->fetchAll(PDO::FETCH_ASSOC);
84
88
 

3

id

2018/10/23 04:07

投稿

yambejp
yambejp

スコア114883

test CHANGED
@@ -70,13 +70,15 @@
70
70
 
71
71
  $sql = "SELECT productarry FROM tbl where 1 ";
72
72
 
73
+ $sql = "and id=? ";
74
+
73
75
  $sql.= "and productarry in('".implode("','",$product)."') ";
74
76
 
75
77
  print $sql;//debug
76
78
 
77
79
  $stm = $pdo->prepare($sql);
78
80
 
79
- $stm->execute();
81
+ $stm->execute([2]);
80
82
 
81
83
  $rows = $stm->fetchAll(PDO::FETCH_ASSOC);
82
84
 

2

調整

2018/10/23 04:07

投稿

yambejp
yambejp

スコア114883

test CHANGED
@@ -72,6 +72,8 @@
72
72
 
73
73
  $sql.= "and productarry in('".implode("','",$product)."') ";
74
74
 
75
+ print $sql;//debug
76
+
75
77
  $stm = $pdo->prepare($sql);
76
78
 
77
79
  $stm->execute();

1

調整

2018/10/23 03:49

投稿

yambejp
yambejp

スコア114883

test CHANGED
@@ -53,3 +53,57 @@
53
53
 
54
54
 
55
55
  ```
56
+
57
+
58
+
59
+ # 調整版
60
+
61
+ SQLの調整と、ヒットしたものだけチェックする処理をいれました
62
+
63
+ ```PHP
64
+
65
+ $rows=[];
66
+
67
+ $product = ['製品A','製品B','製品C','製品D','製品E'];
68
+
69
+ try{
70
+
71
+ $sql = "SELECT productarry FROM tbl where 1 ";
72
+
73
+ $sql.= "and productarry in('".implode("','",$product)."') ";
74
+
75
+ $stm = $pdo->prepare($sql);
76
+
77
+ $stm->execute();
78
+
79
+ $rows = $stm->fetchAll(PDO::FETCH_ASSOC);
80
+
81
+ }catch(PDOException $e){
82
+
83
+ die($e->getMessage());
84
+
85
+ }
86
+
87
+ $data=array_map(function($x){return $x["productarry"];},$rows);
88
+
89
+ echo <<<eof
90
+
91
+ <div class='form-group'>
92
+
93
+ <div class='col-lg-8'>
94
+
95
+ <div class='checkbox' id='product'>
96
+
97
+ <h4>Product Type</h4>
98
+
99
+ eof;
100
+
101
+ foreach ($product as $val) {
102
+
103
+ $checked=(in_array($val,$data)!==false)?" checked":"";
104
+
105
+ echo "<label><input type='checkbox' name='productarry[]' value='{$val}'{$checked}>{$val}</label><span style='margin-right: 25px;'></span>\n";
106
+
107
+ }
108
+
109
+ ```