小さな管理機能を作ってみよう 第27話
初心者向け、PHPプログラム構築講座です。
初心者といっても、PHPの勉強を少し行い、LAMP環境が自分で構築でき、少しアプリケーションを作成しているレベルを対象とします。
まったくの初心者の場合は、わからない部分が出てくると思います。
できるだけ細かく説明は入れていきますが、説明がわからない場合は、PHPやMySQLの初心者講座をご覧ください。
対象のスキルレベル
- LAMP環境の構築
- PHP言語が読める
- HTML, CSS, Javascriptが少しわかる
- Bootstrapのドキュメントをみて、HTMLが書ける
- Ajax(非同期通信)を利用したことがある
- SESSIONを利用したことがある
構築環境
- Windows10
- XAMPP(PHP7.3.2, MariaDB 10.1.38)
部署管理の実装
Dashboardの実装が終了しましたので、部署管理の実装に取り掛かります。
順番としては、PHPコーディング、HTML修正、Javascriptのコーディングの順番で行います。
詳細は以下の通りです。
まずはPHPのコーディングを行っていきます。
Depts.php
api/Depts.phhpファイルを作成します。
作成する内容は、一覧取得、詳細取得(1件取得)、登録、更新、削除です。
api/Depts.php
<?php class Depts { /* traitを使う宣言 */ use Result; /* 全件取得 */ function list($con, $request){ $rs = []; $sql = "select * from depts order by dept_id"; /* プリペアードステートメントを作成します */ if($stmt = $con->prepare($sql)){ /* SQLを実行します */ $stmt->execute(); /* fetchする際のデータの割り当てを設定します */ $stmt->bind_result( $dept_id, $name, $tel, $created, $modified ); /* 全件fetchして変数に割り当てます */ while($stmt->fetch()){ /* fetchが完了したら変数に割り当てます */ $rs[] = [ 'dept_id' => $dept_id, 'name' => $name, 'tel' => $tel, 'created' => $created, 'modified' => $modified ]; } $stmt->close(); /* rsのキーに取得したデータをセットします */ $this->setList('rs', $rs); /* TraitのResultに設定してある成功を実行 */ $this->success(); } /* TraitのResultに設定してある返却用配列を返します */ return $this->result(); } /* 1件取得 */ function get($con, $request){ $rs = []; $sql = "select * from depts where dept_id = ?"; /* プリペアードステートメントを作成します */ if($stmt = $con->prepare($sql)){ /* ?(プレースフォルダー)に値を設定します */ $stmt->bind_param("i", $request['dept_id']); /* SQLを実行します */ $stmt->execute(); /* fetchする際のデータの割り当てを設定します */ $stmt->bind_result( $dept_id, $name, $tel, $created, $modified ); /* 最初の一行だけ取得するので、whileではなくifにしています */ if($stmt->fetch()){ /* fetchが完了したら変数に割り当てます */ $rs = [ 'dept_id' => $dept_id, 'name' => $name, 'tel' => $tel, 'created' => $created, 'modified' => $modified ]; } $stmt->close(); /* rsのキーに取得したデータをセットします */ $this->setList('rs', $rs); /* TraitのResultに設定してある成功を実行 */ $this->success(); } /* TraitのResultに設定してある返却用配列を返します */ return $this->result(); } /* 登録 */ function insert($con, $request){ $sql = "insert into depts(name, tel, modified) values (?, ?, current_timestamp)"; /* プリペアードステートメントを作成します */ if($stmt = $con->prepare($sql)){ /* ?(プレースフォルダー)に値を設定します */ $stmt->bind_param("ss", $request['name'], $request['tel'] ); /* SQLを実行します */ $stmt->execute(); $stmt->close(); /* TraitのResultに設定してある成功を実行 */ $this->success(); } /* TraitのResultに設定してある返却用配列を返します */ return $this->result(); } /* 更新 */ function update($con, $request){ $sql = "update depts set name = ?, tel = ?, modified = current_timestamp where dept_id = ?"; /* プリペアードステートメントを作成します */ if($stmt = $con->prepare($sql)){ /* ?(プレースフォルダー)に値を設定します */ $stmt->bind_param("ssi", $request['name'], $request['tel'], $request['dept_id'] ); /* SQLを実行します */ $stmt->execute(); $stmt->close(); /* TraitのResultに設定してある成功を実行 */ $this->success(); } /* TraitのResultに設定してある返却用配列を返します */ return $this->result(); } /* 削除 */ function delete($con, $request){ $sql = "delete from depts where dept_id = ?"; /* プリペアードステートメントを作成します */ if($stmt = $con->prepare($sql)){ /* ?(プレースフォルダー)に値を設定します */ $stmt->bind_param("i", $request['dept_id']); /* SQLを実行します */ $stmt->execute(); $stmt->close(); /* TraitのResultに設定してある成功を実行 */ $this->success(); } /* TraitのResultに設定してある返却用配列を返します */ return $this->result(); } } ?>
一気に作るとかなり長くなりました。今回はここまとして、次回はHTMLの修正から始めたいと思います。