###前提・実現したいこと
【したいこと】cakephp3 検索条件をselect式にしてand検索したい。
テーブルの全件を下記とします。
URLはhttp://localhost/caketest/photos/とします。
テーブルに対しての検索画面を下記となります。
URLはhttp://localhost/caketest/photos/find/ とします。
現在の機能はName,area,styleテキスト検索にて、条件に一致したものが表示される仕組みです。
【実現したいこと】
・テキスト記述式ではなくてセレクト式にしたい。(Area,Styleそれぞれの)
・2つとも値がセットされていたらAND検索にする。
・Area選択にしてもStyle選択にしても選択されていなかったら【all選択】とする。
・IDに対しての昇順、降順選択ラジオボタンをつける(初期選択は昇順)。
現在のソースを記述します。
PhotosController.php
※findの記述のみです。
php
1 public function find() { 2 $photos = []; 3 if ($this->request->is('post')) { 4 $find = $this->request->data['find']; 5 $photos = $this->Photos->find() 6 ->where(["name like " => '%' . $find . '%']) 7 ->orwhere(["area like " => '%' . $find . '%']) 8 ->orwhere(["style like " => '%' . $find . '%']); 9 } $this->set('msg', "名前orエリアorスタイルで検索出来ます(あいまい検索も可能)"); 10 $this->set('photos', $photos); 11 }
Template/Photos/find.php
php
1<div> 2 <h3>Find photo</h3> 3 <?= $msg ?> 4 <?= $this->Form->create() ?> 5 <fieldset> 6 <?= $this->Form->input('find'); ?> 7 <?= $this->Form->button('Submit') ?> 8 <?= $this->Form->end() ?> 9 </fieldset> 10 <table> 11 <thead> 12 <tr> 13 <th>ID</th> 14 <th>NAME</th> 15 <th>AREA</th> 16 <th>STYLE</th> 17 </tr> 18 </thead> 19 <tbody> 20 <?php foreach ($photos as $photo): ?> 21 <tr> 22 <td><?= h($photo->id) ?></td> 23 <td><?= h($photo->name) ?></td> 24 <td><?= h($photo->area) ?></td> 25 <td><?= h($photo->style) ?></td> 26 </tr> 27 <?php endforeach; ?> 28 </tbody> 29 </table> 30</div>
お手数ではございますが、ご教示いただけましたら幸いでございます。
回答4件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/06/13 12:22