본문 바로가기

웹 개발

웹 개발 공부 일기장 (마이페이지 수정)

일단 마이페이지 수정한 부분을 보여드리겠습니다.

마이페이지의 회원정보

 

보여주는 화면 구성한 php (위의 사진처럼 보여짐.)

<!doctype html>
<html lang="en">

<head>
    <?php
    // 기본적으로 필요한 함수나 모델 가져오기
    require_once('views/includes/head.php');
    require_once('models/user-model.php');
    require_once('config/cookie.php');
    $userId = getCookie($_COOKIE);
    $user = getUser($userId);
    // 만약 로그인되어 있지 않을 경우 로그인 페이지로 이동
    if (!$userId) {
        echo "<script>alert('먼저 로그인 해주세요.'); window.location.href='/login.php';</script>";
        exit;
    }
    ?>
    <link rel="stylesheet" href="static/css/mypage.css">
    <title>Home</title>
</head>

<body>
    <div class="row" id="mypage">
        <?php require_once('mypage-header.php'); ?>
        <div class="col-8">
            <div data-bs-spy="scroll" data-bs-target="#simple-list-example" data-bs-offset="0"
                data-bs-smooth-scroll="true" class="scrollspy-example" tabindex="0">
                <section>
                    <h4 id="simple-list-item-1" style="margin: 0.5rem">회원 정보</h4>
                    <div class="user-info">
                        <ul>
                            <li>이름 :
                                <?= $user['name'] ?>
                            </li>
                            <hr style="margin: 0" />
                            <li>아이디 :
                                <?= $user['id'] ?>
                            </li>
                            <hr style="margin: 0" />
                            <li>이메일 :
                                <?= $user['email'] ?>
                            </li>
                            <hr style="margin: 0" />
                            <li>전화번호 :
                                <?= $user['phone'] ?>
                            </li>
                            <hr style="margin: 0" />
                            <li>생년월일 :
                                <?= $user['birth'] ?>
                            </li>
                        </ul>
                    </div>
                    <div style="margin: 0.5rem" class="end">
                        <ul>
                            <li>
                                <a href="/mypage-edit.php" class="link-secondary">회원정보 변경 |</a>
                                 <!-- 프로필 사진 변경하기위한 부분 띄우기 -->
                                 <button id="profile" type="button" class="btn btn-secondary" data-bs-toggle="modal"
                                    data-bs-target="#staticBackdrop">
                                    프로필 사진 변경
                                </button>
                            </li>
                            <li>
                                <a href="#" class="link-danger">회원탈퇴
                                </a>
                            </li>
                        </ul>
                    </div>
                </section>
                <!-- 프로필 사진 변경 -->
                <form action="" method="POST" enctype="multipart/form-data">
                    <div class="modal fade" id="staticBackdrop" data-bs-backdrop="static" data-bs-keyboard="false"
                        tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <h1 class="modal-title fs-5" id="staticBackdropLabel">프로필 사진 변경</h1>
                                    <button type="button" class="btn-close" data-bs-dismiss="modal"
                                        aria-label="Close"></button>
                                </div>
                                <div class="modal-body">
                                    <div class="input-group mb-3">
                                        <input type="file" class="form-control" id="inputGroupFile02" name="myfile">
                                        <label class="input-group-text" for="inputGroupFile02">Upload</label>
                                    </div>
                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">닫기</button>
                                    <input type="submit" class="btn btn-primary" name="upload-profile"
                                        value="upload"></input>
                                </div>
                            </div>
                        </div>
                    </div>
                </form>
                <?php
                // 프로필 사진 변경하게 되면 uploadProfile 파일을 가져옴.
                if ($_POST['upload-profile'] == 'upload') {
                    require_once('uploadProfile.php');
                }
                ?>
            </div>
        </div>
    </div>

</body>

</html>

 

회원 프로필 사진 변경하기 위해 입력 받은 파일 처리

사진을 가져올 때는 데이터베이스에 저장된 경로로 가져올 수 있음.

<?php
// 업로드된 파일 임시저장 되는 곳
$uploaded_file_name_tmp = $_FILES['myfile']['tmp_name'];
// 업로드된 파일 이름
$uploaded_file_name = $_FILES['myfile']['name'];
// 임시저장된 파일을 우리가 원하는 곳으로 이동시킬 위치
$upload_folder = "images/image/";
$salt = hash('md5', (string)time());

// 업로드된 파일을 각각 구별하기 위한 날짜로 이름 지정
$uploadPath = $upload_folder . $salt . $uploaded_file_name;

// 임시 저장된 파일을 원하는 곳으로 이동
move_uploaded_file($uploaded_file_name_tmp, $uploadPath);

// 데이터베이스에 파일 경로 저장
uploadProfilePhoto($userId, $uploadPath);
?>

 

부족한 부분

- 사진을 변경할 때마다 사진이 저장되어 기존의 사진을 삭제하는 기능을 추가!하면 좋을 것 같음.

- 회원 탈퇴 기능 아직 미구현.

 

마이페이지의 게시글

 

보여지는 화면을 구성한 PHP (위의 사진처럼 보여짐.)

<!doctype html>
<html lang="en">

<head>
    <?php
    require_once('views/includes/head.php');
    require_once('models/user-model.php');
    require_once('models/board-model.php');
    require_once('config/cookie.php');

    $userId = getCookie($_COOKIE);
    $user = getUser($userId);
    if (!$userId) {
        echo "<script>alert('먼저 로그인 해주세요.'); window.location.href='/login.php';</script>";
        exit;
    }
    # board-model에서 클래스를 지정하여 게시글 관리
    $userBoard = new Board($userId, );
    # 유저 아이디로 게시글 가져옴. 해당 유저가 작성한 게시글
    $boardList = $userBoard->getBoardWithId();
    ?>
    <link rel="stylesheet" href="static/css/mypage.css">
    <title>Home</title>
</head>

<body>
    <?php
    if (!$userId) {
        echo "<script>confirm('먼저 로그인 해주세요.')</script>";
        header('location: login.php');
        exit;
    }
    ?>
    <div class="row" id="mypage">
        <?php require_once('mypage-header.php'); ?>
        <div class="col-8">
            <div data-bs-spy="scroll" data-bs-target="#simple-list-example" data-bs-offset="0"
                data-bs-smooth-scroll="true" class="scrollspy-example" tabindex="0">
                <section>
                    <div class="d-grid gap-2" style="width: 60%; margin: 0 auto 3rem auto;">
                        <button class="btn btn-dark" type="button" onclick="location.href='/board.php'">게시판 가기</button>
                    </div>
                    <div id="board-head">
                        <h4 id="simple-list-item-2">나의 게시글</h4>
                        <button class="btn btn-dark" type="button" onclick="location.href='/board-write.php'">글쓰기</button>
                    </div>
                    <div class="user-info">
                        <ul>
                            <?php foreach ($boardList as $board) { ?>
                                <li class="myboard">
                                    <div style="display: flex">제목 :&nbsp;
                                        <!-- 가져온 게시글 정보로 게시글 읽기, 수정, 삭제 기능-->
                                        <a href="/board-detail.php?boardId=<?= $board['idx'] ?>" class="link-secondary">
                                            <?= $board['title'] ?>
                                        </a>
                                    </div>
                                    <div style="display: flex">
                                        <a href="/board-edit.php?boardId=<?= $board['idx'] ?>"
                                            class="link-primary link-offset-2 link-underline-opacity-25 link-underline-opacity-100-hover">
                                            수정
                                        </a>&nbsp;|&nbsp;
                                        <a href="#" class="link-danger">삭제</a>
                                    </div>
                                </li>
                                <hr style="margin: 0" />
                            <?php } ?>
                        </ul>
                    </div>
                </section>
            </div>
        </div>
    </div>

</body>

</html>
<?php 
    require_once('data/database.php');
    class Board {
        private $writer;
        private $title;
        private $content;
        private $view;
        private $date;
    
        // 기본적으로 해당 객체를 만들면 설정될 데이터 지정
        public function __construct($writer, $title='', $content='', $date='') {
            $this->writer = $writer;
            $this->age = $title;
            $this->content = $content;
            $this->view = 0;
            $this->date = $date;
        }

        // 전체 게시글 가져오기
        public static function getBoard() {
            $db_conn = connectDb();
            $query = "select * from boards";
            $result = mysqli_query($db_conn, $query);
            return $result;
        }

        // 게시글 번호로 특정 게시글 가져오기
        public static function getBoardWithIdx($idx) {
            $db_conn = connectDb();
            $query = "select * from boards where idx=$idx";
            $result = mysqli_query($db_conn, $query);
            return mysqli_fetch_array($result);
        }

        // 해당 작성자가 작성한 게시글 가져오기
        public function getBoardWithId() {
            $db_conn = connectDb();
            $writer = $this->writer;
            $query = "select * from boards where writer='$writer'";
            $result = mysqli_query($db_conn, $query);
            return $result;
        }
    }
?>

 

마이페이지는 부족한 부분만 채우면될 것 같아 다음에는 게시판 페이지를 만들어 보겠습니다.