2022年度 サーバサイドプログラミング実習 前期中間試験

問. 次に示す要件のシステムを作成する

言語・フレームワーク

  • PHP7 + Laravel6

データベース

  • MySQL

システム概要

  • 試験の点数を記録するシステム
  • 必要な画面は、点数入力画面と点数一覧画面の2画面
  • 入力画面で生徒番号・氏名・点数を入力すると、データベースの点数テーブルに情報が格納される。バリデーションエラーがあった場合は、入力画面に戻してエラー内容を表示する。
  • 一覧画面で、点数テーブルに格納されている全レコードを一覧表示する。一覧の並び順は点数の降順とする。同点レコードの並び順は、生徒番号の昇順とする。

テーブル定義

点数テーブルの定義は次の通りとする

論理名物理名データ型長さNULL許可主キー
点数IDidinteger(Serial)
生徒番号student_numberinteger
氏名student_namevarchar50
点数scoreinteger
登録日時registed_attimestamp

バリデーションルール

  • 生徒番号は整数で100〜999の範囲とする
  • 氏名は1〜50文字の範囲とする。任意入力とする
  • 点数は整数で0〜100の範囲とする

その他要件

  • 登録日時列には、レコードを追加した日時を格納する

解答例

ルーティング定義

// 入力画面
Route::get('/scores/create', 'ScoresController@create');
// 登録処理
Route::post('/scores', 'ScoresController@store');
// 一覧画面
Route::get('/scores', 'ScoresController@index');

コントローラ

    // 入力画面
    public function create() {
        return view('scores.create');
    }

    // 登録処理
    public function store(Request $request) {
        // バリデーション
        $request->validate([
            'student_number' => 'required|integer|min:100|max:999',
            'student_name' => 'nullable|string|min:1|max:50',
            'score' => 'required|integer|min:0|max:100'
        ]);

        // insert処理
        DB::insert('insert into scores (student_number, student_name, score, registed_at) values (?, ?, ?, ?)',[
            $request->student_number,
            $request->student_name,
            $request->score,
            date('Y-m-d H:i:s')            
        ]);

        return '登録完了';
    }

    // 一覧画面
    public function index() {
        $scores = DB::select('select * from scores order by score DESC, student_number ASC;');

        return view('scores.index',[
            'scores' => $scores
        ]);
    }

ビュー

<html>
<body>
<ul>
    @foreach($errors->all() as $error)
    <li>
        {{ $error }}
    </li>
    @endforeach
</ul>
<form method="POST" action="/scores">
    @csrf
    <p>
        生徒番号<br>
        <input type="text" name="student_number" value="{{ old('student_number') }}">
    </p>
    <p>
        氏名<br>
        <input type="text" name="student_name" value="{{ old('student_name') }}">
    </p>
    <p>
        点数<br>
        <input type="text" name="score" value="{{ old('score') }}">
    </p>
    <p>
        <input type="submit" value="登録">
    </p>
</form>
<body>
</html>
<html>
<body>
<table>
    <thead>
        <tr>
            <th>ID</th>
            <th>生徒番号</th>
            <th>氏名</th>
            <th>点数</th>
            <th>登録日時</th>
        </tr>
    </thead>
    <tbody>
        @foreach($scores as $score)
        <tr>
            <td>{{ $score->id }}</td>
            <td>{{ $score->student_number }}</td>
            <td>{{ $score->student_name }}</td>
            <td>{{ $score->score }}</td>
            <td>{{ $score->registed_at }}</td>
        </tr>
        @endforeach
    </tbody>
</table>
</body>
</html>

SQL

create database test;

create table scores (
    id serial PRIMARY KEY,
    student_number integer NOT NULL,
    student_name varchar(50),
    score integer NOT NULL,
    registed_at timestamp NOT NULL
);

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

Laravel

前の記事

課題2021-01
作品制作

次の記事

2023年度 作品制作