CakePHP1.3.6:テーブルのデータを一覧表示する。
scaffoldを間にはさんでしまったが、以前のログの続きで、テーブルからデータを取得して一覧表示してみる。
以下がその画面。
モデルの作成
/app/modelにcustomer.phpを作成する。
<?php class Customer extends AppModel { var $name = 'Customer'; } ?>
コントローラーの作成
/app/controllersにcustomer_controller.phpを作成する。
<?php class CustomersController extends AppController{ public $name = 'Customers'; var $layout = 'myznala'; function index(){ $this->set('title_for_layout', "モデルのテスト"); $result = $this->Customer->find('all'); $this->set('result',$result); } } ?>
ビューの作成
/app/viewsにcustomersというディレクトリを作成し、indexアクションに対応するindex.ctpを作成する。
<table> <tr> <th>ID</th> <th>名前</th> <th>郵便番号</th> <th>住所</th> <th>電話</th> <th>携帯</th> <th>メール</th> <th>更新日</th> </tr> <?php foreach ($result as $arr){ echo '<tr>'; echo "<td>{$arr['Customer']['id']}</td>"; echo "<td>{$arr['Customer']['name']}</td>"; echo "<td>{$arr['Customer']['zip']}</td>"; echo "<td>{$arr['Customer']['address']}</td>"; echo "<td>{$arr['Customer']['tel']}</td>"; echo "<td>{$arr['Customer']['mobile']}</td>"; echo "<td>{$arr['Customer']['mail']}</td>"; echo "<td>{$arr['Customer']['timestamp']}</td>"; echo '</tr>'; } ?> </table>