前提・実現したいこと
PHPの勉強を始めたばかりで、質問の仕方もよく分かっていないのでとても心苦しいのですが、ご教示いただけますでしょうか
①ページ内検索で、条件にヒットしたら「検索結果はAです」と表示
②複数ヒットした場合「検索結果はAとBと..です」と表示
③ヒットしない場合「該当するものはありません」と表示
④検索条件を入力していない場合は「入力し直してください」と表示
上記実現したいです
求人応募用のサイトを作成中で、検索条件から結果により①〜④を表示したいのですがforeach文に全て記述してしまっているためインスタンスの数だけそれぞれ①〜④まで表示されてしまいます
該当のソースコード
●検索結果表示ページ↓ <?php if($_POST['action']=='select'||$_POST['sense']=='select'):?> <p> 入力し直してください.</p> <?php endif ?> <p>検索結果は <?php foreach($places as $place): ?> <?php if($place->getAction()==$_POST['action'] && $place->getSense()==$_POST['sense']): ?> <a href="<?php echo $place->getLink() ?>"> <?php echo $place->getName() ?>です.</a> <?php else: ?> <p>該当するものがありません</p> <?php endif ?> <?php endforeach ?> ●検索条件入力ページ↓ <form action="places.php" method="post"> <p>どんなところに行きたいですか? <select name="action"> <option><span>select</span></option> <?php $actionArray=['Chill','Exercise','Frolic','Discovery']; $actions=array_unique($actionArray); foreach($actions as $action): ?> <option><?php echo $action; ?></option> <?php endforeach ?> </select> と <select name="sense"> <option><span>select</span></option> <?php $senseArray=['Sight','Hearing','Smell','Touch','Taste']; $senses=array_unique($senseArray); foreach($senses as $sense): ?> <option><?php echo $sense ?></option> <?php endforeach ?> </select> を楽しみたい</p> <br> <p><input class="btn" type="submit" name="results" value="="></p> </form> ●データページ↓ class Place{ protected $name; protected $action; protected $sense; protected $link; protected $country; protected $lowPrice; protected $highPrice; protected $days; public function __construct($name,$action,$sense,$link,$country,$lowPrice,$highPrice,$days) { $this->name = $name; $this->action = $action; $this->sense = $sense; $this->link = $link; $this->country = $country; $this->lowPrice = $lowPrice; $this->highPrice = $highPrice; $this->days = $days; } public function getName(){ return $this->name; } public function getAction(){ return $this->action; } public function getSense(){ return $this->sense; } public function getLink(){ return $this->link; } public function getCountry(){ return $this->country; } public function getLowPrice(){ return $this->lowPrice; } public function getHighPrice(){ return $this->highPrice; } public function getDays(){ return $this->days; } } $sikkim = new Place('Sikkim','Chill','Sight','#sikkim','Indea',156000,324000,'4nights / 7days'); $cocuy = new Place('Cocuy','Exercise','Sight','#cocuy','Colombia',389000,517000,'2nights / 6days'); $berlin =new Place('Berlin','Frolic','Hearing','#berlin','Germany',163000,228000,'5nights / 7days'); $ertaAle = new Place('Erta Ale','Discovery','Smell','#erta-ale','Ethiopia',357000,410000,'5nights / 8days'); $okavangoDerta = new Place('Okavango Derta','Discovery','Touch','#okavango-derta','Botswana',237000,325000,'5nights / 8days'); $dahab = new Place('Dahab','Frolic','Taste','#dahab','Egypt',163000,235000,'4nights / 6days'); $kyoto = new Place('Kyoto','Chill','Sight','#kyoto','Japan',43000,52000,'3nights / 4days'); $places=[$sikkim,$cocuy,$berlin,$ertaAle,$okavangoDerta,$dahab,$kyoto];
試したこと
ifやemptyの中にforeachを記述すれば解決するかと思ったのですが出来ず、知識不足で他の方法が分からず質問した次第です..
どうかよろしくお願い致します
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。