전 글에서는 글 생성을 하였다.
이번엔 글 수정이다.
게시판을 만든다면 꼭 있어야할 기능이다.
물론 실제로 만든다면 당연히 DB가 있는 것이 낫지만 그냥 응용삼아 DB 없이 만들 수도 있다.
일단 전 글에서 만들었던 코드들인 index.php / create.php / create_process.php 부터 나열해본다.
index.php
<?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 "hihihihi";
}
}
function print_list() {
$list = scandir('./data');
$i = 0;
while($i < count($list)) {
if($list[$i] != '.') {
if($list[$i] != '..') {
echo "<a href=\"index.php?id=$list[$i]\"><li>$list[$i]</li></a>\n";
}
}
$i = $i + 1;
}
}
?>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title><?php print_title();?></title>
</head>
<body>
<h1><a href="index.php">WEB</a></h1>
<ol>
<?php
print_list();
?>
<h2>
<?php print_title();?>
</h2>
</ol>
<a href="create.php">create</a>
<?php if(isset($_GET['id'])) {
?>
<a href="update.php?id=<?=$_GET['id']?>">update</a>
<?php } ?>
<h2>
<?php print_description();?>
</h2>
</body>
</html>
create.php
form 부분 제외하고 index.php와 똑같다고 보아도 된다.
<?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 "hihihihi";
}
}
function print_list() {
$list = scandir('./data');
$i = 0;
while($i < count($list)) {
if($list[$i] != '.') {
if($list[$i] != '..') {
echo "<a href=\"index.php?id=$list[$i]\"><li>$list[$i]</li></a>\n";
}
}
$i = $i + 1;
}
}
?>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title><?php print_title();?></title>
</head>
<body>
<h1><a href="index.php">Create</a></h1>
<ol>
<?php
print_list();
?>
<h2>
<?php print_title();?>
</h2>
</ol>
<a href="create.php">create</a>
<form action="create_process.php" method="post">
<p>
<input type="text" name="title" placeholder="title">
</p>
<p>
<textarea name="description" placeholder="description" id="" cols="30" rows="10"></textarea>
</p>
<p>
<input type="submit">
</p>
</form>
<h2>
<?php print_description();?>
</h2>
</body>
</html>
create_process.php
얘는 전과 변함이 없다.
<?php
file_put_contents('data/'.$_POST['title'],$_POST['description']);
header('Location: /index.php?id='.$_POST['title']);
?>
아래로는 글 수정을 위한 코드이다.
update.php
다른 코드와 다른 점은 form 부분에서 찾을 수 있다.
<?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 "hihihihi";
}
}
function print_list() {
$list = scandir('./data');
$i = 0;
while($i < count($list)) {
if($list[$i] != '.') {
if($list[$i] != '..') {
echo "<a href=\"index.php?id=$list[$i]\"><li>$list[$i]</li></a>\n";
}
}
$i = $i + 1;
}
}
?>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title><?php print_title();?></title>
</head>
<body>
<h1><a href="index.php">WEB</a></h1>
<ol>
<?php
print_list();
?>
<h2>
<?php print_title();?>
</h2>
</ol>
<a href="create.php">create</a>
<?php if(isset($_GET['id'])) {
?>
<a href="update.php?id=<?=$_GET['id']?>">update</a>
<?php } ?>
<h2>
<form action="update_process.php" method="post">
<input type="hidden" name="old_title" value="<?=$_GET['id']?>"> //old_title 이라는 태그가 있기에 클라이언트가 제목을 수정 가능하다. hidden 속성이기 때문에 클라이언트에게는 보이지 않는다.
<p>
<input type="text" name="title" placeholder="title" value="<?php print_title(); ?>"> // value에 있는 것은 기존에 클라이언트가 적었던 내용이 들어간다.
</p>
<p>
<textarea name="description" placeholder="description" id="" cols="30" rows="10" value=""><?php print_description();?></textarea> //textarea 태그 바로 사이에 php 부분을 넣어야만 클라이언트가 적었던 내용이 들어간다.
</p>
<p>
<input type="submit">
</p>
</form>
</body>
</html>
update_process.php
<?php
rename('data/'.$_POST['old_title'],'data/'.$_POST['title']); //전의 제목을 새로운 제목으로 다시 지을 수 있는 php 함수가 rename이다.
file_put_contents('data/'.$_POST['title'], $_POST['description']); //description 내용을 바꿀 수 있는 명령어이다.
?>
댓글