index.php
<?php
// once ) 한번만 불러오겠다
include_once 'include/header.php';
?>
<div id="contents">
<h2>도서리스트</h2>
<ul>
<?php
// 💚 책 리스트 보여줌
creatList();
?>
<!-- <li><a href="#">리스트1</a></li>
<li><a href="#">리스트2</a></li>
<li><a href="#">리스트3</a></li>
<li><a href="#">리스트4</a></li> -->
</ul>
<h2>
<?php
// 💚 책 제목 보여줌
printTitle();
?>
</h2>
<div id="bookInfo">
<?php
// 💚 책 내용 보여줌
printDesc();
// 리스트 클릭시 계속 바껴야됨
// echo file_get_contents('data/멈추고 호흡하고 선택하라');
?>
<!-- php 안에 html 넣기 -->
<?php
if(isset($_GET['id'])) {
?>
<!-- html 내용 넣기위해 php 짜르기 -->
<ul>
<!-- 수정 ) edit.php 글제목, 파일이름-->
<li><a href="edit.php?id=<?=$_GET['id']?>">글 수정</a></li>
<!-- 삭제 ) 어떤 글인지 전달해줘야됨 GET id 가 어떤 글인지 알고있음 -->
<li >
<form action="process/delete_process.php" method="post">
<!-- type="hidden" 보이면 안됨, 전달해주기 위해 만든것 -->
<input type="hidden" name="id" value="<?=$_GET['id']?>">
<input type="submit" value="글 삭제">
</form>
</li>
</ul>
<?php
}
?>
</div>
</div>
<?php
// once ) 한번만 불러오겠다
include_once 'include/footer.php';
?>
edit.php
<?php include 'include/header.php' ?>
<!-- create.php 그대로 복사함 -->
<div id="contents">
<h2>도서 수정하기</h2>
<form action="process/edit_process.php" method="post">
<table>
<tr>
<td>글 제목</td>
<td>
<!-- 바뀐 제목 전송 -->
<input type="hidden" name="old_id" value="<?=$_GET['id']?>">
<!-- required 내용을 입력해야 전송 -->
<input type="text" name="id" required value="<?=$_GET['id']?>">
</td>
</tr>
<tr>
<td>글 내용</td>
<td>
<textarea name="description" id="description" cols="30" rows="10" required>
<?php
echo file_get_contents('data/'.$_GET['id']);
?>
</textarea>
</td>
</tr>
<tr>
<td colspan="2">
<button type="submit">글 적기</button>
<button type="reset">취소</button>
</td>
</tr>
</table>
</form>
</div>
<?php include 'include/footer.php' ?>
create.php
<?php include_once 'include/header.php' ?>
<div id="contents">
<h2>도서 등록하기</h2>
<form action="process/write_process.php" method="post">
<table>
<tr>
<td>글 제목</td>
<td>
<!-- required 내용을 입력해야 전송 -->
<input type="text" name="id" required>
</td>
</tr>
<tr>
<td>글 내용</td>
<td>
<textarea name="description" id="description" cols="30" rows="10" required>
</textarea>
</td>
</tr>
<tr>
<td colspan="2">
<button type="submit">글 적기</button>
<button type="reset">취소</button>
</td>
</tr>
</table>
</form>
</div>
<?php include_once 'include/footer.php' ?>
process > write_process.php
<?php
// 💚 등록된 도서 전송
// post 전송으로 전송된 데이터는 슈퍼글로벌 $_POST 전역변수로 받음
// $_POST 는 연관배열인 $_POST['keyname']
// var_dump($_POST);
$id = $_POST['id'];
$desc = $_POST['description'];
file_put_contents('../data/'.$id, $desc);
// 리다이렉션
header('Location:../index.php');
?>
process > edit_process.php
<?php
// 파일 이름 변경하기 rename(파일이름, 변경할이름)
rename('../data/'.$_POST['old_id'], '../data/'.$_POST['id']);
// 파일 내용 변경하기 file_put_contents(파일이름, 내용)
file_put_contents('../data/'.$_POST['id'], $_POST['description']);
header('Location:../index.php');
?>
process > delete_process.php
<?php
// 💚 파일 삭제하기 unlink(파일경로)
unlink('../data/'.$_POST['id']);
header('Location:../index.php');
?>
include > header.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>횽 Blog</title>
<link rel="stylesheet" href="./css/style.css">
</head>
<body>
<?php
// scandir() 폴더에 있는 항목을 연관배열로 반환
// 0 => "." , 1 => ".."
// 배열길이 count(배열변수)
// array(6) { [0]=> string(1) "." [1]=> string(2) ".."
// [2]=> string(35) "멈추고 호흡하고 선택하라"
// [3]=> string(54) "아티스트 웨이, 마음의 소리를 듣는 시간"
// [4]=> string(56) "일 잘하는 사람의 시간은 다르게 흘러간다"
// [5]=> string(38) "일하면서 성장하고 있습니다" }
// $lists = scandir('data/');
// var_dump($lists);
// 💚 책 리스트를 html출력
function creatList() {
$lists = scandir('data/');
for($i=0; $i<count($lists); $i++) {
// 변수
$title = $lists[$i];
if($lists[$i] != "." && $lists[$i] !="..") {
// 쿼리스트링 ) ? 뒤에 데이터 전달
echo "<li><a href='index.php?id=${title}'>${title}</a></li>";
}
}
}
// 💚 책 제목을 html출력
function printTitle() {
// $_GET['id'] 이 존재하는지 ?
if(isset($_GET['id'])) { // 존재하면 값 넣어줌(출력)
echo $_GET['id'];
} else { // 존재하지 않을시
echo "Blog";
}
}
// 💚 책 내용을 html출력
function printDesc() {
if(isset($_GET['id'])) {
echo file_get_contents('data/'.$_GET['id']);
} else {
echo "저희 Blog를 방문해주셔서 감사합니다.";
}
}
?>
<div id="wrap">
<header>
<h1><a href="index.php">Green Blog</a></h1>
<ul>
<li><a href="index.php">홈</a></li>
<li><a href="create.php">글쓰기</a></li>
</ul>
</header>
include > footer.php
<footer>
<p>copyright (c) all rights reserved.</p>
<h1>Green Blog</h1>
</footer>
</div>
</body>
</html>
css > style.css
*{margin: 0; padding: 0; box-sizing: border-box;}
li{list-style: none;}
a{color: inherit; text-decoration: none;}
/* @import url('https://fonts.googleapis.com/css2?family=Single+Day&display=swap'); */
:root {
--main-color: tomato;
}
body {
/* font-family: 'Single Day', cursive, sans-serif; */
font-size: 14px;
line-height: 1.6;
color: #222;
}
#wrap {
width: 100%;
max-width: 1200px;
margin: 0 auto;
}
header {
border-bottom: 1px solid #ccc;
display: flex;
justify-content: space-between;
height: 120px;
align-items: center;
}
header ul {
display: flex;
}
header ul li {
background: var(--main-color);
margin: 6px;
padding: 8px 24px;
border-radius: 6px;
color: #fff;
}
footer {
border-top: 1px solid #ccc;
padding: 30px 0;
}
#contents {
padding: 100px 0;
}
#contents > h2 {
padding-bottom: 30px;
position: relative;
}
#contents ul {
margin-bottom: 100px;
}
#contents h2::after {
content: "";
display: block;
position: absolute;
background: var(--main-color);
width: 50px;
height: 4px;
top: -10px;
left: 0;
}
#contents > ul > li {
line-height: 40px;
border-bottom: 1px solid #ccc;
padding-left: 20px;
}
#bookInfo ul {
display: flex;
padding: 30px;
}
#bookInfo ul li {
background: var(--main-color);
margin: 6px;
padding: 5px 15px;
border-radius: 6px;
color: #fff;
}
#contents table {
width: 100%;
border-top: 2px dotted #ccc;
border-collapse: collapse;
}
#contents table td {
border-bottom: 1px dotted #ccc;
padding: 16px;
}
#contents table input {
border: none;
outline: none;
background: pink;
width: 80%;
line-height: 40px;
}
#contents table textarea {
border: none;
outline: none;
background: pink;
width: 80%;
line-height: 40px;
}
#contents table button {
border: none;
outline: none;
background: var(--main-color);
margin: 6px;
padding: 5px 15px;
border-radius: 6px;
color: #fff;
}
#bookInfo ul li input {
border: none;
outline: none;
background: transparent;
color: #fff;
}
'PHP' 카테고리의 다른 글
[PHP] book2 (0) | 2023.04.05 |
---|---|
[PHP] book1 (0) | 2023.04.05 |
[PHP] file (0) | 2023.04.05 |
[PHP] include (0) | 2023.04.05 |
[PHP] session, cookie (0) | 2023.04.05 |