FuelPHP + MySQL で PHP Webアプリケーションを作ってみる ― データ表示処理
データベースからデータを取得して画面表示する処理を実装します。
データベース接続設定
まず、FuelPHPでデータベースに接続するための設定を行います。
app/config/development/db.php に下記を設定します。
<?php return array( 'default' => array( 'type' => 'pdo', 'connection' => array( 'dsn' => 'mysql:host=localhost;dbname=user_management', 'username' => 'root', 'password' => '********', // 設定したパスワード 'persistent' => false, 'compress' => false, ), 'identifier' => '`', 'table_prefix' => '', 'charset' => 'utf8', 'enable_cache' => true, 'profiling' => false, 'readonly' => false, ), );
Model
データベースからデータを取得する機能は、Modelと呼ばれるクラスで実装します。
今回は、とりあえず条件なしに全件取得する処理を実装します。
DEPT_MSTからデータを取得するModel: app/classes/model/deptmst.php
<?php namespace Model; class DeptMst extends \Model { public static function get_all() { $results = \DB::query('SELECT * FROM dept_mst')->execute(); return $results->as_array(); } }
USER_MSTからデータを取得するModel: app/classes/model/usermst.php
<?php namespace Model; class UserMst extends \Model { public static function get_users() { $query = 'SELECT d.DEPT_NAME ' . ' , u.USER_CD ' . ' , u.USER_NAME ' . ' , u.ENTER_DATE ' . ' FROM user_mst u ' . ' INNER JOIN dept_mst d ' . ' ON d.dept_cd = u.dept_cd '; $results = \DB::query($query)->execute(); return $results->as_array(); } }
Controller
Modelを使ってデータを取得し、画面表示するためのViewを呼び出す処理をControllerとして実装します。
app/classes/controller/list.php
<?php use \Model\DeptMst; use \Model\UserMst; class Controller_List extends Controller public function action_index() { $data['depts'] = DeptMst::get_all(); // DEPT_MSTからデータ取得 $data['users'] = UserMst::get_users(); // USER_MSTからデータ取得 return Response::forge(View::forge('list/index', $data)); } }
View
Controllerから呼び出されるViewを実装します。
Viewでは、渡されたデータをHTMLとして表示する役割を担います。
前回はプルダウンの内容や一覧のデータは固定で記載していましたが、ここでは取得したデータを表示するようになっています。
app/views/list/index.php
<!DOCTYPE html> <head> <meta charset="utf-8"> <title>社員管理システム</title> <?php echo Asset::css('style.css'); ?> </head> <body> <div class="header"> <h1>社員管理システム</h1> </div> <div class="main"> <table class="search"> <tr> <th>所属</th> <td> <select class="dept_code"> <option selected>(選択してください)</option> <?php foreach ($depts as $dept) { echo '<option>'.$dept['DEPT_NAME'].'</option>'; } ?> </select> </td> </tr> <tr> <th>社員コード</th> <td><input type="text" class="emp_code small" value="" /></td> </tr> <tr> <th>社員名</th> <td><input type="text" class="emp_name wide" value="" /></td> </tr> </table> <div class="action_area"> <button class="search_button">検索</button> </div> <table class="list"> <tr> <th style="width: 100px;">所属</th> <th style="width: 100px;">社員コード</th> <th style="width: 200px;">社員名</th> <th style="width: 100px;">入社</th> </tr> <?php foreach ($users as $user) { echo '<tr>'. '<td>'.$user['DEPT_NAME'].'</td>'. '<td>'.$user['USER_CD'].'</td>'. '<td>'.$user['USER_NAME'].'</td>'. '<td>'.date('Y/m/d', strtotime($user['ENTER_DATE'])).'</td>'. '</tr>'; } ?> </table> </div> </body> </html>
動作確認
ブラウザで「http://localhost/UserManagement/public/list/」を確認してみます。
MySQLから取得したデータが表示されるようになっていることがわかると思います。
次回はどんな仕組みで動いてるか、簡単に理解していきます。
その後、検索機能の実装を行っていきます。