「これ、絶対もっと効率的に書ける筈」、と思いながら結局、POSTされた際のsqlのパターンを全部書いてしまった。
終わってる。死にたい。。
concatの値がnullがある場合は、ifnullを使います。
CONCAT(IFNULL(username, ”), IFNULL(message, ”))
<?php $dsn = "mysql:dbname=mail;host=localhost"; $user = "hoge"; $password = "hogehoge"; try { $dbh = new PDO($dsn, $user, $password); } catch (PDOException $e){ print('connection failed:'.$e->getMessage()); } if(!empty($_POST["search"]) and !empty($_POST["gender"]) and !empty($_POST["age"]) and !empty($_POST["job"]) and !empty($_POST["area"])){ $sql = "select * from users where CONCAT(IFNULL(username, ''), IFNULL(message, '')) like '%".$_POST["search"]."%' and gender = '".$_POST["gender"]."' and age = '".$_POST["age"]."' and job = '".$_POST["job"]."' and area = '".$_POST["area"]."' order by id desc"; }elseif(!empty($_POST["search"]) and !empty($_POST["age"]) and !empty($_POST["job"]) and !empty($_POST["area"])){ $sql = "select * from users where CONCAT(IFNULL(username, ''), IFNULL(message, '')) like '%".$_POST["search"]."%' and age = '".$_POST["age"]."' and job = '".$_POST["job"]."' and area = '".$_POST["area"]."' order by id desc"; }elseif(!empty($_POST["search"]) and !empty($_POST["gender"]) and !empty($_POST["job"]) and !empty($_POST["area"])){ $sql = "select * from users where CONCAT(IFNULL(username, ''), IFNULL(message, '')) like '%".$_POST["search"]."%' and gender = '".$_POST["gender"]."' and job = '".$_POST["job"]."' and area = '".$_POST["area"]."' order by id desc"; }elseif(!empty($_POST["search"]) and !empty($_POST["gender"]) and !empty($_POST["age"]) and !empty($_POST["area"])){ $sql = "select * from users where CONCAT(IFNULL(username, ''), IFNULL(message, '')) like '%".$_POST["search"]."%' and gender = '".$_POST["gender"]."' and area = '".$_POST["area"]."' order by id desc"; }elseif(!empty($_POST["search"]) and !empty($_POST["gender"]) and !empty($_POST["age"]) and !empty($_POST["job"])){ $sql = "select * from users where CONCAT(IFNULL(username, ''), IFNULL(message, '')) like '%".$_POST["search"]."%' and gender = '".$_POST["gender"]."' and age = '".$_POST["age"]."' and job = '".$_POST["job"]."' order by id desc"; }elseif(!empty($_POST["search"]) and !empty($_POST["job"]) and !empty($_POST["area"])){ $sql = "select * from users where CONCAT(IFNULL(username, ''), IFNULL(message, '')) like '%".$_POST["search"]."%' and job = '".$_POST["job"]."' and area = '".$_POST["area"]."' order by id desc"; }elseif(!empty($_POST["search"]) and !empty($_POST["age"]) and !empty($_POST["area"])){ $sql = "select * from users where CONCAT(IFNULL(username, ''), IFNULL(message, '')) like '%".$_POST["search"]."%' and age = '".$_POST["age"]."' and area = '".$_POST["area"]."' order by id desc"; }elseif(!empty($_POST["search"]) and !empty($_POST["age"]) and !empty($_POST["job"])){ $sql = "select * from users where CONCAT(IFNULL(username, ''), IFNULL(message, '')) like '%".$_POST["search"]."%' and age = '".$_POST["age"]."' and job = '".$_POST["job"]."' order by id desc"; }elseif(!empty($_POST["search"]) and !empty($_POST["gender"]) and !empty($_POST["area"])){ $sql = "select * from users where CONCAT(IFNULL(username, ''), IFNULL(message, '')) like '%".$_POST["search"]."%' and gender = '".$_POST["gender"]."' and area = '".$_POST["area"]."' order by id desc"; }elseif(!empty($_POST["search"]) and !empty($_POST["gender"]) and !empty($_POST["job"])){ $sql = "select * from users where CONCAT(IFNULL(username, ''), IFNULL(message, '')) like '%".$_POST["search"]."%' and gender = '".$_POST["gender"]."' and job = '".$_POST["job"]."' and order by id desc"; }elseif(!empty($_POST["search"]) and !empty($_POST["gender"]) and !empty($_POST["age"])){ $sql = "select * from users where CONCAT(IFNULL(username, ''), IFNULL(message, '')) like '%".$_POST["search"]."%' and gender = '".$_POST["gender"]."' and age = '".$_POST["age"]."' order by id desc"; }elseif(!empty($_POST["search"]) and !empty($_POST["gender"])){ $sql = "select * from users where CONCAT(IFNULL(username, ''), IFNULL(message, '')) like '%".$_POST["search"]."%' and gender = '".$_POST["gender"]."' order by id desc"; }elseif(!empty($_POST["search"]) and !empty($_POST["age"])){ $sql = "select * from users where CONCAT(IFNULL(username, ''), IFNULL(message, '')) like '%".$_POST["search"]."%' and age = '".$_POST["age"]."' order by id desc"; }elseif(!empty($_POST["search"]) and !empty($_POST["job"])){ $sql = "select * from users where CONCAT(IFNULL(username, ''), IFNULL(message, '')) like '%".$_POST["search"]."%' and job = '".$_POST["job"]."' order by id desc"; }elseif(!empty($_POST["search"]) and !empty($_POST["area"])){ $sql = "select * from users where CONCAT(IFNULL(username, ''), IFNULL(message, '')) like '%".$_POST["search"]."%' and area = '".$_POST["area"]."' order by id desc"; }elseif(!empty($_POST["search"])){ $sql = "select * from users where CONCAT(IFNULL(username, ''), IFNULL(message, '')) like '%".$_POST["search"]."%' order by id desc"; }elseif(!empty($_POST["gender"]) and !empty($_POST["age"]) and !empty($_POST["job"]) and !empty($_POST["area"])){ $sql = "select * from users where gender = '".$_POST["gender"]."' and age = '".$_POST["age"]."' and job = '".$_POST["job"]."' and area = '".$_POST["area"]."' order by id desc"; }elseif(!empty($_POST["gender"]) and !empty($_POST["job"]) and !empty($_POST["area"])){ $sql = "select * from users where gender = '".$_POST["gender"]."' and job = '".$_POST["job"]."' and area = '".$_POST["area"]."' order by id desc"; }elseif(!empty($_POST["gender"]) and !empty($_POST["age"]) and !empty($_POST["area"])){ $sql = "select * from users where gender = '".$_POST["gender"]."' and age = '".$_POST["age"]."' and area = '".$_POST["area"]."' order by id desc"; }elseif(!empty($_POST["gender"]) and !empty($_POST["age"]) and !empty($_POST["job"])){ $sql = "select * from users where gender = '".$_POST["gender"]."' and age = '".$_POST["age"]."' and job = '".$_POST["job"]."' order by id desc"; }elseif(!empty($_POST["gender"]) and !empty($_POST["area"])){ $sql = "select * from users where gender = '".$_POST["gender"]."' and area = '".$_POST["area"]."' order by id desc"; }elseif(!empty($_POST["gender"]) and !empty($_POST["job"])){ $sql = "select * from users where gender = '".$_POST["gender"]."' and job = '".$_POST["job"]."' order by id desc"; }elseif(!empty($_POST["gender"]) and !empty($_POST["age"])){ $sql = "select * from users where gender = '".$_POST["gender"]."' and age = '".$_POST["age"]."' order by id desc"; }elseif(!empty($_POST["gender"])){ $sql = "select * from users where gender = '".$_POST["gender"]."' order by id desc"; }elseif(!empty($_POST["age"]) and !empty($_POST["job"]) and !empty($_POST["area"])){ $sql = "select * from users where age = '".$_POST["age"]."' and job = '".$_POST["job"]."' and area = '".$_POST["area"]."' order by id desc"; }elseif(!empty($_POST["age"]) and !empty($_POST["area"])){ $sql = "select * from users where age = '".$_POST["age"]."' and area = '".$_POST["area"]."' order by id desc"; }elseif(!empty($_POST["age"]) and !empty($_POST["job"])){ $sql = "select * from users where age = '".$_POST["age"]."' and job = '".$_POST["job"]."' order by id desc"; }elseif(!empty($_POST["age"])){ $sql = "select * from users where age = '".$_POST["age"]."' order by id desc"; }elseif(!empty($_POST["job"]) and !empty($_POST["area"])){ $sql = "select * from users where job = '".$_POST["job"]."' and are = '".$_POST["are"]."' order by id desc"; }elseif(!empty($_POST["job"])){ $sql = "select * from users where job = '".$_POST["job"]."' order by id desc"; }elseif(!empty($_POST["area"])){ $sql = "select * from users where area = '".$_POST["area"]."' order by id desc"; }else{ $sql = "select * from users order by id desc"; } $stmt = $dbh->query($sql); $result = $stmt->fetchAll(PDO::FETCH_ASSOC); // var_dump($result); ?> <style> #img{ position: relative; } #img img{ vertical-align:top; } #icon{ vertical-align:top; float:left; } #profile{ display: inline; } </style> <form action="" method="post"><input type="search" name="search" placeholder="ユーザー名もしくは一言メッセージから探す"> <input type="submit" value="検索"><br> 性別:<input type="radio" name="gender" value="男性">男性 <input type="radio" name="gender" value="女性">女性 年齢: <select name="age"> <option value="">選択してください</option> <option value="20歳未満">20歳未満</option> <option value="20-24歳">20-24歳</option> <option value="25-29歳">25-29歳</option> <option value="30-34歳">30-34歳</option> <option value="35-39歳">35-39歳</option> <option value="40-44歳">40-44歳</option> <option value="45-49歳">45-49歳</option> <option value="50-54歳">50-54歳</option> <option value="55-59歳">55-59歳</option> <option value="60-64歳">60-64歳</option> <option value="65-69歳">65-69歳</option> <option value="70-74歳">70-74歳</option> <option value="75-79歳">75-79歳</option> <option value="80歳以上">80歳以上</option> </select> 職業: <select name="job"> <option value="">選択してください</option> <option value="公務員">公務員</option> <option value="経営者・役員">経営者・役員</option> <option value="会社員">会社員</option> <option value="自営業">自営業</option> <option value="自由業">自由業</option> <option value="専業主婦">専業主婦</option> <option value="パート・アルバイト">パート・アルバイト</option> <option value="学生">学生</option> <option value="その他">その他</option> </select> 登録地域: <select name="area"> <option value="">選択してください</option> <option value="北海道">北海道</option> <option value="青森県">青森県</option> <option value="岩手県">岩手県</option> <option value="宮城県">宮城県</option> <option value="秋田県">秋田県</option> <option value="山形県">山形県</option> <option value="福島県">福島県</option> <option value="茨城県">茨城県</option> <option value="栃木県">栃木県</option> <option value="群馬県">群馬県</option> <option value="埼玉県">埼玉県</option> <option value="千葉県">千葉県</option> <option value="東京都">東京都</option> <option value="神奈川県">神奈川県</option> <option value="新潟県">新潟県</option> <option value="富山県">富山県</option> <option value="石川県">石川県</option> <option value="福井県">福井県</option> <option value="山梨県">山梨県</option> <option value="長野県">長野県</option> <option value="岐阜県">岐阜県</option> <option value="静岡県">静岡県</option> <option value="愛知県">愛知県</option> <option value="三重県">三重県</option> <option value="滋賀県">滋賀県</option> <option value="京都府">京都府</option> <option value="大阪府">大阪府</option> <option value="兵庫県">兵庫県</option> <option value="奈良県">奈良県</option> <option value="和歌山県">和歌山県</option> <option value="鳥取県">鳥取県</option> <option value="島根県">島根県</option> <option value="岡山県">岡山県</option> <option value="広島県">広島県</option> <option value="山口県">山口県</option> <option value="徳島県">徳島県</option> <option value="香川県">香川県</option> <option value="愛媛県">愛媛県</option> <option value="高知県">高知県</option> <option value="福岡県">福岡県</option> <option value="佐賀県">佐賀県</option> <option value="長崎県">長崎県</option> <option value="熊本県">熊本県</option> <option value="大分県">大分県</option> <option value="宮崎県">宮崎県</option> <option value="鹿児島県">鹿児島県</option> <option value="沖縄県">沖縄県</option> <option value="海外">海外</option> </select><br> </form> <?php $i = 0; foreach($result as $value){ if($i < 20){ if(!empty($value["icon"])){ $picon = $value["icon"]; echo "<img src=\"".$picon."\" id=\"icon\" width=\"48px\" height=\"48px\">"; } elseif($value['gender'] == "男性") { echo "<img src=\"asset/img/male.png\" id=\"icon\">"; } else{ echo "<img src=\"asset/img/female.png\" id=\"icon\">"; } echo "<div id=\"profile\"><a href=\"?compose=new&to=".$value['username']."\">".$value['username'] ."</a>:".$value['age']." <span id=\"fs-s\">".$value['gender'] ."</span><br>"; if(!empty($value["message"])){ echo $value['job']."(".$value['area'].") <span id=\"fs-s\">一言メッセージ:「".$value["message"]."」</span></div><br><br>"; } else { echo $value['job']."(".$value['area'].")</div><br><br>"; } $i++; } }
挙動としては、思い通りに動いてくれるんだが、自分にがっかりするな。。
つなげると、
おおおお、基本機能はほぼ出来たか!
次は、スタイリングかな