会員一覧と会員検索
会員管理を作ります。
最初に、いまのままだと見づらいので、一覧(indexアクション)の整形をしておきます。
admin_index.ctp を開いて、不要な列を削除します。
今は、
<table cellpadding="0" cellspacing="0">
<tr>
<th><?php echo $this->Paginator->sort('id');?></th>
<th><?php echo $this->Paginator->sort('email');?></th>
<th><?php echo $this->Paginator->sort('password');?></th>
<th><?php echo $this->Paginator->sort('type_id');?></th>
<th><?php echo $this->Paginator->sort('birthday');?></th>
<th><?php echo $this->Paginator->sort('img1');?></th>
<th><?php echo $this->Paginator->sort('img2');?></th>
<th><?php echo $this->Paginator->sort('created');?></th>
<th><?php echo $this->Paginator->sort('modified');?></th>
<th class="actions"><?php __('Actions');?></th>
</tr>
こうなっていますが、password img1 img2 modified あたりはなくてもよいと思います。
(クライアント次第ですが)
対応する td の方も削除しておきます。
と、一見して、登録日(created)がDBの日付データそのままで出力されていて見づらいです。
ということで、 members_controller.php の helper にさらに ‘Time’ を追加します。
class MembersController extends AppController {
var $name = 'Members';
var $helpers = array('Javascript', 'Time');
で、ビューに戻り、誕生日と登録日は、
こうなっているのを
<td><?php echo $member['Member']['birthday']; ?> </td>
<td><?php echo $member['Member']['created']; ?> </td>
こうします。
<td><?php echo $this->Time->format($format='Y/m/d', $member['Member']['birthday']); ?> </td>
<td><?php echo $this->Time->format($format='Y/m/d', $member['Member']['created']); ?> </td>
ついでに、
<div class="actions"> ... </div>
の中のアクションリストは不要なので丸ごと削除しておきます。
最後に paginate の設定を入れておきます。
members_controller.php の上の方に、
var $paginate = array(
'limit' => 15,
'order' => array(
'Member.id' => 'desc'
),
);
を入れます。15件で改ページ、デフォルトの並び順を会員のID降順にします。
引き続き、会員検索のページの準備をします。
一覧の admin_index.ctp にそのまま検索フォームをつけてしまうというのもありかとは思いますが、とりあえず今回は別ビュー、別アクションにします。
admin_index.ctp を別の名前で保存で admin_serch.ctp を作ります。
で、フォーム開始を
<?php echo $this->Form->create(array("action" => "search", "type" => "post")); ?>
として、
<table>
<tr>
<th>入会日</th>
<td>
<?php echo $this->Form->year("from", '2011','2012'); ?>年
<?php echo $this->Form->month("from",null,array('monthNames'=>false)); ?>月
<?php echo $this->Form->day("from",null); ?>日
~
<?php echo $this->Form->year("to", '2011','2012'); ?>年
<?php echo $this->Form->month("to",null,array('monthNames'=>false)); ?>月
<?php echo $this->Form->day("to",null); ?>日
</td>
</tr>
<tr>
<th>メールアドレス</th>
<td>
<?php echo $this->Form->text("email"); ?>
</td>
</tr>
<tr>
<th>種別</th>
<td>
<?php echo $this->Form->select("type_id", $types); ?>
</td>
</tr>
<tr>
<th>好きな物</th>
<td>
<?php echo $this->Form->input('favorites', array('multiple'=>'checkbox', 'label' => false)); ?>
</td>
</tr>
</table>
<?php echo $this->Form->end(' 検索 '); ?>
このような検索フォーム部分を追加します。
さらにその下に
<?php $this->Paginator->options(array('url' => $searchword )); ?>
peginator のオプションを入れておきます。
これは peginator に検索キーワードを渡すためのものです。
とりあえず、members_controller.php に以下のアクションと追加しておきます。
function admin_search() {
$this->Member->recursive = 0;
$this->set('members', $this->paginate());
$types = $this->Member->Type->find('list');
$favorites = $this->Member->Favorite->find('list');
$this->set(compact('types', 'favorites'));
}
