글삭제
delete 버튼을 만들고 그 버튼을 누르면 바로 삭제되게끔 할 것이다
그렇기에 delete라는 링크는 필요없고 delete_process만 있으면 된다
파일을 지우는건 unlink()
삭제 또한 링크가 아닌 POST방식으로 해줘야한다
<?php
function print_title(){
if(isset($_GET['id'])){
echo $_GET['id'];
} else{
echo "Welcome";
}
}
function print_description(){
if(isset($_GET['id'])){
echo file_get_contents("data/".$_GET['id']);
} else {
echo "Hello, PHP";
}
}
function print_list(){
$list = scandir('./data');
$i = 0;
while($i < count($list)){
if($list[$i] != '.'){
if($list[$i] != '..') {
echo "<li><a href=\"index.php?id=$list[$i]\">$list[$i]</a></li>\n";
}
}
$i = $i + 1;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>
<?php
print_title();
?>
</title>
</head>
<body>
<h1><a href="index.php">WEB</a></h1>
<ol>
<?php
print_list();
?>
</ol>
<a href="create.php">create</a>
<?php if(isset($_GET['id'])) { ?>
<a href="update.php?id=<?=$_GET['id']?>">update</a>
<form action="delete_process.php" method="post">
<input type="hidden" name="id" value="<?=$_GET['id']?>">
<input type="submit" value="delete">
</form>
<?php } ?>
<h2>
<?php
print_title();
?>
</h2>
<?php
print_description();
?>
</body>
</html>
이제 delete_process.php를 보자
<?php
unlink('data/'.$_POST['id']);
header('Location: /index.php');
?>
unlink 즉 그 id에 해당하는 데이터 디렉토리 안의 파일을 삭제하고
인덱스 홈페이지로 이동해라 라는 뜻이다
파일로 모듈화 - require
리팩토링 작업을 해줄 것이다
좋은 코드를 만드는 아주 쉬운 실천 방법은 중복을 제거하는 것
재사용할만한 코드나 로직을 잘 정리정돈 해놓으면 도서관이라고 한다
제일 상단에 항상 중복되는 코드가 있다
<?php
function print_title(){
if(isset($_GET['id'])){
echo $_GET['id'];
} else{
echo "Welcome";
}
}
function print_description(){
if(isset($_GET['id'])){
echo file_get_contents("data/".$_GET['id']);
} else {
echo "Hello, PHP";
}
}
function print_list(){
$list = scandir('./data');
$i = 0;
while($i < count($list)){
if($list[$i] != '.'){
if($list[$i] != '..') {
echo "<li><a href=\"index.php?id=$list[$i]\">$list[$i]</a></li>\n";
}
}
$i = $i + 1;
}
}
?>
lib 이라는 디렉토리를 만들고 그 안에 print.php 라는 파일을 만들어준다
그리고 그 안에 이 겹치는 부분의 코드를 입력해준다
index.php create.php update.php 파일에 겹치는 부분을 지우고
<?php
require('lib/print.php');
?>
이것을 입력해준다
보여지는 것과 관련된 view 라는 디렉토리를 만들어 준다
view 디렉토리 안에 bottom 이라는 파일을 만들어주고
</body>
</html>
이 중복되는 코드들을 집어넣어준다
이도 똑같이 index create update 에
<?php
require('view/bottom.php');
?>
입력해준다
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>
<?php
print_title();
?>
</title>
</head>
<body>
<h1><a href="index.php">WEB</a></h1>
<ol>
<?php
print_list();
?>
</ol>
이 부분도 겹친다 이것은 top.php라고 view 디렉토리 안에 만들어주고
똑같이
<?php
require('view/top.php');
?>
해주면 된다
top.php 파일에서 print_title(); 이 어떤 것을 가리키는지 정확히 알지 못하므로
lib디렉토리에서 print.php파일을 모듈로 불러와주자
<?php
require('lib/print.php');
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>
<?php
print_title();
?>
</title>
</head>
<body>
<h1><a href="index.php">WEB</a></h1>
<ol>
<?php
print_list();
?>
</ol>
이렇게 하면
Cannot redeclare print_title() 즉 중복되었다고 오류가 뜬다
왜냐하면 index.php에
<?php
require('lib/print.php');
require('view/top.php');
?>
print.php를 불러오고 또 top.php 또한 print.php를 불러오므로 중복 오류가 뜬 것이다
이때는
require_once 를 써주자
중복되게 쓰지 못하게 하고 한번만 쓰게 해준다