zoaseo
To Infinity And Beyond
zoaseo
전체 방문자
오늘
어제
  • 분류 전체보기 (763)
    • 개발이 좋아서 (381)
      • SAP가 좋아서 (3)
      • Java가 좋아서 (42)
      • Spring이 좋아서 (50)
      • JPA가 좋아서 (0)
      • QueryDSL이 좋아서 (26)
      • Docker가 좋아서 (7)
      • Redis가 좋아서 (7)
      • AWS가 좋아서 (5)
      • CI/CD가 좋아서 (6)
      • Troubleshooting이 좋아서 (4)
      • Kotlin이 좋아서 (7)
      • SQL이 좋아서 (6)
      • HTTP가 좋아서 (21)
      • JavaScript가 좋아서 (30)
      • TypeScript가 좋아서 (6)
      • Vue가 좋아서 (21)
      • Flutter가 좋아서 (61)
      • React가 좋아서 (20)
      • Redux(React)가 좋아서 (2)
      • Angular가 좋아서 (22)
      • HTML이 좋아서 (9)
      • CSS가 좋아서 (15)
      • PHP가 좋아서 (9)
      • Illustrator가 좋아서 (2)
    • 노력이 좋아서 (169)
    • 결과물이 좋아서 (14)
    • 코딩연습이 좋아서 (168)
      • 이론이 좋아서 (62)
      • SQL이 좋아서 (90)
    • 유용한 사이트가 좋아서 (28)
    • Github (2)

인기 글

티스토리

hELLO · Designed By 정상우.
zoaseo

To Infinity And Beyond

<step48>'php_Blog만들기'
노력이 좋아서

<step48>'php_Blog만들기'

2022. 5. 26. 14:14

1)

style.css

@import url('https://fonts.googleapis.com/css2?family=Varela+Round&display=swap');

* { margin: 0; padding: 0; box-sizing: border-box;}
li { list-style: none;}
a { color: inherit; text-decoration: none;}
:root {
    --main-color: tomato;
}
body {
    font-family: 'Varela Round', sans-serif;
    font-size: 16px;
    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-top: 30px;
}
#contents {
    padding: 120px 0;
}
#contents h2 {
    padding-bottom: 30px;
    position: relative;
}
#contents h2::after {
    content: "";
    display: block;
    position: absolute;
    background: var(--main-color);
    width: 50px;
    height: 4px;
    top: -16px;
    left: 0;
}
#contents ul {
    margin-bottom: 100px;
}
#contents > ul > li {
    line-height: 50px;
    border-bottom: 1px dotted #ccc;
    padding-left: 20px;
}
#bookinfo ul {
    display: flex;
    padding: 30px 0;
}
#bookinfo ul li {
    background: var(--main-color);
    margin: 6px;
    padding: 8px 24px;
    border-radius: 6px;
    color: #fff;
    font-size: 13.5px;
}
#bookinfo input {
    border: none;
    outline: none;
    background-color: transparent;
    color: #fff;
}
#contents table {
    width: 100%;
    border-top: 1px dotted #ccc; 
    border-collapse: collapse;
}
#contents table td {
    border-bottom: 1px dotted #ccc;
    padding: 16px;
}
#contents table input {
    border: none;
    outline: none;
    background: rgb(253, 232, 228);
    width: 80%;
    line-height: 40px;
}
#contents table textarea {
    border: none;
    outline: none;
    background: rgb(253, 232, 228);
    width: 80%;
    line-height: 40px;
}
#contents table button {
    border: none;
    outline: none;
    background: var(--main-color);
    margin: 6px;
    padding: 8px 24px;
    border-radius: 6px;
    color: #fff;
}

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>Document</title>
    <link rel="stylesheet" href="./css/style.css">
    <title>블로그</title>
</head>
<body>
    <?php
        //scandir() 폴더에 있는 항목을 연관배열로 반환
        //0 => "." 1 => ".."
        //배열길이 count(배열변수)
        function createList(){
            $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 printtTitle(){
            //$_GET['id']가 존재하는지 확인
            if(isset($_GET['id'])){
                echo $_GET['id'];
            }else {
                echo '책 제목';
            }
        } 
        //책 내용을 html출력
        function printDesc(){
            //$_GET['id']가 존재하는지 확인
            if(isset($_GET['id'])){
                echo file_get_contents('data/'.$_GET['id']);  //파일 내용 불러오기
            }else {
                echo '내용입니다.';
            }
        }
    ?>
    <div id="wrap">
        <header>
            <h1><a href="index.php">Blog</a></h1>
            <ul>
                <li><a href="index.php">홈</a></li>
                <li><a href="create.php">글쓰기</a></li>
            </ul>
        </header>

index.php

<?php
    include 'include/header.php'
?>
        <div id="contents">
            <h2>도서리스트</h2>
            <ul>
                <?php createList(); ?>
            </ul>
            <h2><?php printtTitle(); ?></h2>
            <div id="bookinfo">
                <?php printDesc(); ?>
                <?php
                    if(isset($_GET['id'])){ 
                ?>         
                <ul>
                    <li><a href="edit.php?id=<?=$_GET['id']?>">글수정</a></li>
                    <li>
                        <form action="process/delete_process.php" method="post">
                            <input type="hidden" name="id" value="<?=$_GET['id']?>">
                            <input type="submit" value="글삭제">
                        </form>    
                    </li>
                </ul>
                <?php
                    }
                ?>
            </div>
        </div>
<?php
    include 'include/footer.php'
?>

footer.php

<footer>
            <p>copyright (c) all rights reserved.</p>
            <h1>Blog</h1>
        </footer>
    </div>
</body>
</html>

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><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' ?>

edit.php

<?php include_once 'include/header.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']?>">
                        <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_once 'include/footer.php' ?>

write_process.php

<?php
    // var_dump($_POST);
    // post전송으로 전송된 데이터는 슈퍼글로벌 $_POST 전역변수로 받음
    // $_POST는 연관배열일 $_POST['keyname']
    $id = $_POST['id'];
    $desc = $_POST['description'];

    file_put_contents('../data/'.$id, $desc);
    // 리다이렉션
    header('Location:../index.php');
?>

edit_process.php

<?php
    //파일이름 변경하기 rename(파일이름, 변경할 이름)
    //내용변경하기 file_put_contents(파일이름, 내용)
    rename('../data/'.$_POST['old_id'],'../data/'.$_POST['id']);
    file_put_contents('../data/'.$_POST['id'],$_POST['description']);
    header('Location:../index.php');
?>

delete_process.php

<?php
    //파일 삭제하기 unlink(파일경로)
    unlink('../data/'.$_POST['id']);
    header('Location:../index.php');
?>

'노력이 좋아서' 카테고리의 다른 글

<step49>'도서목록, 등록, 수정, 삭제 만들기'  (0) 2022.05.27
<step48>'php_sql연동'  (0) 2022.05.26
<step48>'php_empty(), isset(), include'  (0) 2022.05.26
<step47>'php_GET, POST, file_get_contents(), file_put_contents()'  (0) 2022.05.25
<step47>'php_연산자, 제어문, 조건문'  (0) 2022.05.25

    티스토리툴바