PHP 글 생성 (DB 없이)
PHP

PHP 글 생성 (DB 없이)

by nocagklhw 2019. 12. 29.

index.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>
    <h2>
        <?php print_description();?>
    </h2>
</body>
</html>

 

create.php

글을 쓸 수 있는 페이지

//create.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

create.php에 썼던 내용을 data 폴더 내에 파일로 생성해주고, 생성 뒤 바로 글로 돌아가게 하는 페이지

//create.process.php
<?php
file_put_contents('data/'.$_POST['title'],$_POST['description']);
header('Location: /index.php?id='.$_POST['title']);
?>

 

 

 

댓글


TOP

TEL. 02.1234.5678 / 경기 성남시 분당구 판교역로