CakePHPでつまってるところまとめ
とりあえず色々なところを参考にサンプルを書いてみた
- view部分
search.ctp
<h2>検索画面</h2> <?php echo $form->create(array('action' => 'search', 'type' => 'post'));?> ショップID <?php echo $form->text('Blog.shopid', $options=array('size' => '40', 'maxlength' => '40'));?> <br /> 合計値 <?php echo $form->text('Blog.amount', $options=array('size' => '10', 'maxlength' => '10'));?> 円以上 <div>こっから検索結果</div> <?php echo $paginator->counter(array( 'format' => __('Page %page% of %pages%, showing %current% records out of %count% total, starting on record %start%, ending on %end%', true) )); ?></p> <?php /* 2008/12/25修正 この例の$searchwordはArrayデータなのでStringを引数にとるurlencodeだとうまく動かないため、コントローラ側でurlencodeするように修正しました。 */ ?> <?php $paginator->options(array('url' => $searchword )); ?> <table cellpadding="0" cellspacing="0"> <tr> <th><?php echo $paginator->sort('id');?></th> <th><?php echo $paginator->sort('売り上げ年月','orderdate');?></th> <th><?php echo $paginator->sort('ショップID','shopid');?></th> <th><?php echo $paginator->sort('合計金額','amount');?></th> </tr> <?php foreach ($blogs as $blog): ?> <tr> <td> <?php echo h( $blog['Blog']['id'] ); ?> </td> <td> <?php echo h( $blog['Blog']['orderdate']) ; ?> </td> <td> <?php echo h( $blog['Blog']['shopid'] ); ?> </td> <td> <?php echo h( $blog['Blog']['amount'] ); ?> </td> </tr> <?php endforeach; ?> </table> </div> <div class="paging"> <?php echo $paginator->prev('<< '.__('previous', true), array(), null, array('class'=>'disabled'));?> | <?php echo $paginator->numbers();?> <?php echo $paginator->next(__('next', true).' >>', array(), null, array('class'=>'disabled'));?> </div>
- モデル部分
blog.php
<?php class Blog extends AppModel{ var $name = 'Blogs'; var $useTable = 'blogs'; } ?>
- コントローラ部分
blogs_controller.php
<?php class BlogsController extends AppController{ var $name = 'Blogs'; var $helpers = array('Html','Form','paginator'); var $paginate = array('order'=> array('Blog.orderdate' => 'desc'), 'limit' => 1, ); function search(){ $this->Blog->recursive = 0; if(!empty($this->data)){ // $shopid = $this->data['Blog']['shopid']; // $amount = $this->data['Blog']['amount']; $search['shopid'] = $this->data['Blog']['shopid']; $search['amount'] = $this->data['Blog']['amount']; }elseif(count($this->passedArgs)){ $search = $this->passedArgs; $this->data['Blog']['shopid'] = $search['shopid']; $this->data['Blog']['amount'] = $search['amount']; } $search_list = array( 'shopid', 'amount', ); $search_value = array(); foreach($search_list as $value){ $search_value[$value] = urlencode($search[$value]); } $condition = array(); $condition = array( 'shopid like' => '%' . $search['shopid'] .'%', 'amount >=' => $search['amount'], ); $this->set('amount_list',$this->Blog->find('all')); debug($this->Blog->find('all')); $this->set('searchword', $search_value); $this->set('blogs',$this->paginate($condition)); } function searchresult(){ $this->Blog->recursive = 0; if(!empty($this->data)){ // $shopid = $this->data['Blog']['shopid']; // $amount = $this->data['Blog']['amount']; $search['shopid'] = $this->data['Blog']['shopid']; $search['amount'] = $this->data['Blog']['amount']; }elseif(count($this->passedArgs)){ $search = $this->passedArgs; $this->data['Blog']['shopid'] = $search['shopid']; $this->data['Blog']['amount'] = $search['amount']; } $search_list = array( 'shopid', 'amount', ); $search_value = array(); foreach($search_list as $value){ $search_value[$value] = urlencode($search[$value]); } $condition = array(); $condition = array( 'shopid like' => '%' . $search['shopid'] .'%', 'amount >=' => $search['amount'], ); $this->set('searchword', $search_value); $this->set('blogs',$this->paginate($condition)); } }
テーブルは
blogsテーブルで
中身が
id,shopid,orderdate,amountです
表示を二列にしたい
今ビュー部分で検索結果を一列で表示してるけど二列にしたい。CSSでなんとかなるのかな。
それともロジックでなんとかなるのかしら。
ラジオボタンのセット
サンプルがてらidも検索対象にしようと思いプルダウンかラジオボタンでもどちらでもいいから実装しようと思った。しかしフォームの中に
<?php echo $form->radio('Blog.amount', $amount_list, array( 'value' => 1 ) ); ?>
といれたらArray,Array,Array...とならんでいた。
このときのamount_listは
Array
(
[0] => Array
(
[Blog] => Array
(
[id] => 1
[shopid] => testuser2
[orderdate] => 2010-08-26 22:10:06
[amount] => 100
))
[1] => Array
(
[Blog] => Array
(
[id] => 2
[shopid] => testu3
[orderdate] => 2010-08-26 22:10:06
[amount] => 200
))
[2] => Array
(
[Blog] => Array
(
[id] => 3
[shopid] => hoge3
[orderdate] => 2010-08-26 22:11:40
[amount] => 1000
))
[3] => Array
(
[Blog] => Array
(
[id] => 4
[shopid] => hoge4
[orderdate] => 2010-08-26 22:11:40
[amount] => 399
))
[4] => Array
(
[Blog] => Array
(
[id] => 5
[shopid] => hoge3
[orderdate] => 2010-08-26 22:46:49
[amount] => 1000
))
[5] => Array
(
[Blog] => Array
(
[id] => 6
[shopid] => hoge3
[orderdate] => 2010-08-26 22:46:49
[amount] => 10000
))
)
だった。結果をidの配列で欲しいのだけれど...。
良い方法を知っている方がいれば教えていただきたいです...。