[CI] Update

Grace Goh·2023년 4월 2일
0

PHP

목록 보기
14/19

컨트롤러에 edit()를 만든다.

    // show를 복사해서 만든다.
    public function edit($idx)
    {
        // ['edit']로 만들어서
        $data['edit'] = $this->board_model->get($idx);
        // views로 보내면 변수 $edit로 소환할 수 있다. ($direct_list의 역할)
        $this->load->view('board/edit', $data);
    }  

['edit']인 이유는

아래와 같이 views에서 $edit로 전달하기 위해서다.

# views/board/edit.php

글 수정 화면

// 약식기호는 <?= $edit->idx; ?>
<form name="rafa" action="/board/update/<?php echo $edit->idx; ?>" method="post">

    // RESTful한 방식으로 api 전달하겠다
    <input type="hidden" name="_method" value="PUT" />
    <table board="1">
        <tr>
            <th>제목</th>
            <td>
                <input type="text" name="title" value="<?= $edit->title; ?>" />
            </td>
        </tr>
        <tr>
            <th>내용</th>
            <td>
                <textarea name="contents" rows="8"><?= $edit->contents; ?></textarea>
            </td>
        </tr>
        <tr>
            <th>내용</th>
            <td>
            <th colspan="2">
                <input type="submit" value=" 수정하기 " />
                <a href="/board">목록</a>
            </th>
            </td>
        </tr>
    </table>
</form>

라우터를 설정한다.

# app/config/routes.php

(...)

$route['board'] = 'board/index';
$route['board/create'] = 'board/create';
$route['board/store']['post'] = 'board/store';

// number로만 전달하도록. 인덱스 인자로 전달된다.
$route['board/edit/(:num)'] = 'board/edit/$1';
$route['board/update/(:num)']['put'] = 'board/update/$1';

게시판 리스트에 수정 버튼을 추가한다.

# views/board/list.php

게시판 리스트입니다.

<table border="1">
    // 이 구조로 만들겠다
    <tr>
        <th>제목</th>
        <th>작성일</th>
        <th>관리</th>
    </tr>
    <?php
    foreach ($articles as $art) {
    ?>
        // foreach에 의해 tr이 반복적으로 돈다.
        <tr>
            <td><?= $art->title; ?></td>
            <td><?= $art->regdate; ?></td>
            <td>
                <a href="/board/show/<?= $art->idx; ?>">보기</a>
                <a href="/board/edit/<?= $art->idx; ?>">수정</a> # 추가
            </td>
        </tr>
    <?php
    }
    ?>
</table>

<a href="/board/create">글쓰기</a>

컨트롤러에 store()와 유사한 update()를 만든다.

# controllers/Board.php


    // store를 복사
    public function update($idx)
    {
        $this->form_validation->set_rules('title', 'Title', 'required');
        $this->form_validation->set_rules('contents', 'Contents', 'required');

        // 실행run할 때 true라면
        if ($this->form_validation->run()) {
            // store를 실행하고 board/create로 redirect한다.
            $this->board_model->update($idx);
            redirect('/board');
        } else {
            echo "Error";
        }
    }

모델을 만든다.

# models/Board_model.php


    public function update($idx) {
        $data = [
            'title' => $this->input->post('title'),
            'contents' => $this->input->post('contents'),
            // 'regdate' => $this->input->post('title'), 는 수정되면 안 됨. 최초등록일자.
        ];

        $result = $this->db->where('idx', $idx)->update('boards', $data);
        return $result;
    }
profile
Español, Inglés, Coreano y Python

0개의 댓글