前提・実現したいこと
formから飛んできた値をみてそれに該当する値を表示する機能で
その際にformからの値は1つではないためphpで持っている配列に複数個の値をもたせたい。
該当のソースコード
html
1<form id="form" name="search" action="" method="POST"> 2 <div class="form-list"> 3 <p>タイプ</p> 4 <input type="radio" id="type01" name="type" value="a"><label for="type01">a</label> 5 <input type="radio" id="type02" name="type" value="b"><label for="type02">b</label> 6 </div> 7 <div class="form-list"> 8 <p>カラー</p> 9 <input type="radio" id="color01" name="color" value="red"><label for="color01">赤</label> 10 <input type="radio" id="color02" name="color" value="blue"><label for="color02">青</label> 11 </div> 12 <button class="submit" value="check">検索</button> 13 </form>
php
1$type = filter_input( INPUT_POST, "type" ); 2$color = filter_input( INPUT_POST, "color" ); 3 4$data = [ 5 "test"=>[ 6 [ 7 "type"=>"a", 8 "color"=>"blue" 9 ], 10 [ 11 "type"=>"b" 12 "color"=>"red" 13 ], 14 [ 15 "type"=>"a" 16 "color"=>"blue" 17 ], 18 [ 19 "type"=>"b" 20 "color"=>"red" 21 ] 22 ] 23]; 24 25$result = array_filter($data["test"],function($x) use($type,$color) { 26 return (is_null($type) || $x["type"] == $type) &&; 27 (is_null($color) || $x["color"] == $color); 28}); 29if ($result == null) { 30 print <<<eof 31 <div class="empty"> 32 <p>チェックされてない</p> 33 </div> 34eof; 35} else { 36 array_walk($result,function($x){ 37 print <<<eof 38 <p>{$x["type"]}</p> 39 <p>{$x["color"]}</p> 40eof; 41}); 42}
試したこと
現状上記のコードで動くには動くのですが、
$dataの中のtypeやcolorの中に
php
1$data = [ 2 "test"=>[ 3 [ 4 "type"=>["a","b"], 5 "color"=>"red" 6 ], 7 [ 8 "type"=>"a", 9 "color"=>["red","blue"] 10 ]...
のような感じで試して見たのですが、
うまく検索されず「チェックされていない」が表示されてしまいます。
どのようなデータの形にしたら良いのでしょうか?
ご教示のほど宜しくお願いします。
回答2件
あなたの回答
tips
プレビュー