うさぎのイラスト

ネットアンサー55備忘録

web技術を書いていきます

一覧表示と条件表示

-2017年06月17日- cakephpcake
テーブル情報一覧表示 〇〇Controller.php (ページネーションの設定も含まれているみたいです)
	public function index() {
		$this->set('msg', 'メッセージ代入できます。');
		$persons = [];
		$persons = $this->Persons->find();

		$this->set('persons', $persons);
	}
Templete/〇〇/index.ctp
<?php
/**
  * @var \App\View\AppView $this
  * @var \App\Model\Entity\Person[]|\Cake\Collection\CollectionInterface $persons
  */
?>
<nav class="large-3 medium-4 columns" id="actions-sidebar">
    <ul class="side-nav">
        <li class="heading"><?= __('Actions') ?></li>
        <li><?= $this->Html->link(__('New Person'), ['action' => 'add']) ?></li>
    </ul>
</nav>
<div class="persons index large-9 medium-8 columns content">
    <h3><?= __('Persons') ?></h3>
    <table cellpadding="0" cellspacing="0">
        <thead>
            <tr>
                <th scope="col"><?= $this->Paginator->sort('id') ?></th>
                <th scope="col"><?= $this->Paginator->sort('age') ?></th>
                <th scope="col" class="actions"><?= __('Actions') ?></th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($persons as $person): ?>
            <tr>
                <td><?= $this->Number->format($person->id) ?></td>
                <td><?= $this->Number->format($person->age) ?></td>
                <td class="actions">
                    <?= $this->Html->link(__('View'), ['action' => 'view', $person->id]) ?>
                    <?= $this->Html->link(__('Edit'), ['action' => 'edit', $person->id]) ?>
                    <?= $this->Form->postLink(__('Delete'), ['action' => 'delete', $person->id], ['confirm' => __('Are you sure you want to delete # {0}?', $person->id)]) ?>
                </td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</div>
---------------------------------------------- 条件における一覧表示の〇〇Controller.phpの書き方
	public function index() {
		$this->set('msg', 'メッセージ代入できす。');
		$persons = [];
			$persons = $this->Persons->find()
	 
				//全部検索
				//$persons = $this->Persons->find();
	
				//指名検索
				//->where(["Persons.name" => '山田']);
	
				//あいまい検索
				//->where(["Persons.name like " => '%山%']);
	
				//and検索
				//->where(["Persons.name" => '山田'])
				//->where(["Persons.age" => '22']);
	
				//or検索
				//->where(["Persons.name" => '山田'])
				//->orwhere(["Persons.age" => '21']);
				
				//or検索とあいまい検索
			   	//->orWhere(["Persons.id" => 7])
			   	//->orWhere(["Persons.mail like " => '%fff%']);

		$this->set('persons', $persons);
	}