리스트에 삭제 버튼을 만든다.
# views/board/list.php
<table border="1">
<tr>
<th>제목</th>
<th>작성일</th>
<th>관리</th>
</tr>
<?php
foreach ($articles as $art) {
?>
<tr>
<td><?= $art->title; ?></td>
<td><?= $art->regdate; ?></td>
<td>
<a href="/board/show/<?= $art->idx; ?>">보기</a>
<a href="/board/edit/<?= $art->idx; ?>">수정</a>
<a href="/board/delete/<?= $art->idx; ?>">삭제</a> # 추가
</td>
</tr>
<?php
}
?>
</table>
<a href="/board/create">글쓰기</a>
라우터를 추가한다.
$route['board/delete/(:num)']['delete'] = 'board/delete/$1';
컨트롤러에 delete 함수를 만든다.
// edit를 복사해서 만든다.
public function delete($idx)
{
// delete 모델에 $idx값을 전달한다.
$item = $this->board_model->delete($idx);
redirect("/board");
// 삭제되면 리스트로 돌아간다.
}
(컨트롤러를 만들었으니) 모델로 간다.
public function delete($idx) {
// 이렇게 쓰는 것을 db 쿼리라고 한다. ("테이블"에, 배열('키'=>$밸류)로 보낸다)
$result = $this->db->delete("boards", array('idx' => $idx));
return $result;
}
모델 안에 where, update, result, delete.. CI가 기본 제공