반응형

 

 

페이지를 여러개 만들었는데 가만 보니 페이지마다 공통된 부분들이 있다.

 

상단에는

 

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>

    <title>게시판 리스트</title>
  </head>
  <body>
  <div class="col-md-8" style="margin:auto;padding:20px;">

 

이부분이고 하단에는 

 

</div>

</body>
</html>

 

이부분이다. 앞뒤로 이부분을 위치시키고 이 사이에 다른 코드를 넣어주면 화면이 완성된다. 이부분을 페이지마다 떼어내서 파일로 만든뒤 include 시켜 보겠다.

 

우선 새로운 폴더와 파일을 만든다. 아래처럼 inc폴더를 만들고 그안에 루트에 있던 dbcon.php 파일을 올긴다. 그리고 header와 footer에 사용할 파일을 만든다.

 

이렇게 보이면 된다.

 

헤더에는 

header.php

 

<?php
include $_SERVER["DOCUMENT_ROOT"]."/inc/dbcon.php";
?>

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
    <title>게시판</title>
  </head>
  <body>
  <div class="col-md-8" style="margin:auto;padding:20px;">

 

푸터는...

footer.php

 

</div>
</body>
</html>

 

이렇게 입력한다.

 

헤더에는 디비 연결 을 위한 include가 또 들어가있다. include할때는 반드시 $_SERVER["DOCUMENT_ROOT"] 절대경로 루트를 넣어서 위치를 지정해주자.

 

자 이제 헤더와 푸터가 준비됐으니 기존 파일들을 수정해 include 시켜주자.

 

index.php

<?php
include $_SERVER["DOCUMENT_ROOT"]."/inc/header.php";

$result = $mysqli->query("select * from board") or die("query error => ".$mysqli->error);
while($rs = $result->fetch_object()){
    $rsc[]=$rs;
}
?>

<table class="table">
<thead>
<tr>
    <th scope="col">번호</th>
    <th scope="col">글쓴이</th>
    <th scope="col">제목</th>
    <th scope="col">등록일</th>
</tr>
</thead>
<tbody>
<?php
$i=1;
foreach($rsc as $r){
?>
    <tr>
        <th scope="row"><?php echo $i++;?></th>
        <td><?php echo $r->userid?></td>
        <td><a href="/view.php?bid=<?php echo $r->bid;?>"><?php echo $r->subject?></a></td>
        <td><?php echo $r->regdate?></td>
    </tr>
<?php }?>
</tbody>
</table>

<div class="col-md-4" style="float:right;padding:20px;">
    <a href="write.php"><button type="button" class="btn btn-primary">등록</button><a>
</div>

<?php
include $_SERVER["DOCUMENT_ROOT"]."/inc/footer.php";
?>

 

view.php

<?php
include $_SERVER["DOCUMENT_ROOT"]."/inc/header.php";

$bid=$_GET["bid"];
$result = $mysqli->query("select * from board where bid=".$bid) or die("query error => ".$mysqli->error);
$rs = $result->fetch_object();
?>
      <h3 class="pb-4 mb-4 fst-italic border-bottom" style="text-align:center;">
        - 게시판 보기 -
      </h3>

      <article class="blog-post">
        <h2 class="blog-post-title"><?php echo $rs->subject;?></h2>
        <p class="blog-post-meta"><?php echo $rs->regdate;?> by <a href="#"><?php echo $rs->userid;?></a></p>

        <hr>
        <p>
          <?php echo $rs->content;?>
        </p>
        <hr>
      </article>

      <nav class="blog-pagination" aria-label="Pagination">
        <a class="btn btn-outline-primary" href="/index.php">목록</a>
        <!-- <a class="btn btn-outline-secondary" href="#">답글</a> -->
      </nav>

<?php
include $_SERVER["DOCUMENT_ROOT"]."/inc/footer.php";
?>

 

write.php

<?php
include $_SERVER["DOCUMENT_ROOT"]."/inc/header.php";
?>
        <form method="post" action="write_ok.php">
            <div class="mb-3">
            <label for="exampleFormControlInput1" class="form-label">제목</label>
                <input type="text" name="subject" class="form-control" id="exampleFormControlInput1" placeholder="제목을 입력하세요.">
            </div>
            <div class="mb-3">
            <label for="exampleFormControlTextarea1" class="form-label">내용</label>
            <textarea class="form-control" id="exampleFormControlTextarea1" name="content" rows="3"></textarea>
            </div>
            <button type="submit" class="btn btn-primary">등록</button>
        </form>
<?php
include $_SERVER["DOCUMENT_ROOT"]."/inc/footer.php";
?>

 

write_ok.php

<?php
include $_SERVER["DOCUMENT_ROOT"]."/inc/dbcon.php";

$subject=$_POST["subject"];
$content=$_POST["content"];
$userid="hong";//userid는 없어서 임의로 넣어줬다.
$status=1;//status는 1이면 true, 0이면 false이다.

$sql="insert into board (userid,subject,content) values ('".$userid."','".$subject."','".$content."')";
$result=$mysqli->query($sql) or die($mysqli->error);

if($result){
    echo "<script>location.href='/index.php';</script>";
    exit;
}else{
    echo "<script>alert('글등록에 실패했습니다.');history.back();</script>";
    exit;
}
?>

 

/member/signup.php

<?php
include $_SERVER["DOCUMENT_ROOT"]."/inc/header.php";
?>

        <form class="row g-3 needs-validation" method="post" action="signup_ok.php">
        <div class="col-12">
            <label for="validationCustom01" class="form-label">이름</label>
            <input type="text" class="form-control" id="userame" name="username" placeholder="" required>
        </div>
        <div class="col-12">
            <label for="validationCustom02" class="form-label">아이디</label>
            <input type="text" class="form-control" id="userid" name="userid" placeholder="" required>
        </div>
        <div class="col-12">
            <label for="validationCustom02" class="form-label">비밀번호</label>
            <input type="password" class="form-control" id="passwd" name="passwd" placeholder="" required>
        </div>
        <div class="col-12">
            <label for="validationCustomUsername" class="form-label">이메일</label>
            <div class="input-group has-validation">
            <span class="input-group-text" id="inputGroupPrepend">@</span>
            <input type="email" class="form-control" id="email" name="email" placeholder="" required>
            </div>
        </div>
       
        <div class="col-12">
            <button class="btn btn-primary" type="submit">가입하기</button>
        </div>
        </form>
   
<?php
include $_SERVER["DOCUMENT_ROOT"]."/inc/footer.php";
?>

 

/member/signup_ok.php

<?php
include $_SERVER["DOCUMENT_ROOT"]."/inc/dbcon.php";

$userid=$_POST["userid"];
$username=$_POST["username"];
$email=$_POST["email"];
$passwd=$_POST["passwd"];
$passwd=hash('sha512',$passwd);

$sql="INSERT INTO members
        (userid, username, email, passwd)
        VALUES('".$userid."', '".$username."', '".$email."', '".$passwd."')";
$result=$mysqli->query($sql) or die($mysqli->error);

if($result){
    echo "<script>alert('가입을 환영합니다.');location.href='/index.php';</script>";
    exit;
}else{
    echo "<script>alert('회원가입에 실패했습니다.');history.back();</script>";
    exit;
}

?>

 

이렇게 바꿔준다.

반응형

+ Recent posts