로그인을 하면 session에 회원 아이디와 회원 이름이 저장된다.

이후 게시판을 이용할 때 회원의 아이디와 이름이 필요 할 때 마다 session에 저장된 아이디와 이름을 꺼내 쓸 수 있다.


데이터베이스 board 테이블 data


[connect_db.php]

1
2
3
4
5
6
7
8
9
<?php
//MYSQL 접속 정보
$db = mysqli_connect("127.0.0.1:3306","DB사용자명","DB비번","DB");
if(!$db){
    die("Error ".mysqli_connect_error());
}
//한글
mysqli_set_charset($db'UTF8');
?>
 
 

[main.php] - 로그인후 첫화면

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
    include "connect_db.php";
 
    session_start();    //session 가져오기
    //session에 데이터가 없다면 로그인 화면으로 GO
    if (!isset($_SESSION['userID'])) {
        header('Location : http://wamp서버ip:80/test/login/login');
    }
 
    //한 화면에 보여주는 글 목록 개수
    $page_size=5;
 
    //현재 페이지 기억
    if (isset($_GET["current_page"])) {
        $current_page = $_GET["current_page"];
    }else{
        $current_page=1;
    }
 
    //시작 페이지 정하기
    $begin = ($current_page -1* $page_size;
?>
<!DOCTYPE html>
<html>
    <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
    <script src="/test/js/bootstrap.js"></script>
    <head>
        <meta charset ="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>게시판 리스트</title>
        <link rel ="stylesheet" href="/test/css/bootstrap.css">
    </head>
    <body>
        <div class = "container">
            <table class = "table table-hover">
                <thead>
                    <tr>
                        <!--게시판 속성-->
                        <th>글번호</th>
                        <th>제목</th>
                        <th>작성자</th>
                        <th>날짜</th>
                        <th>조회수</th>
                    </tr>
                </thead>
                <?php       // DB에 저장된 글목록을 idx의 내림차순으로 가져와 페이지에 집어넣기
                $sql = "SELECT * FROM board ORDER BY idx DESC limit $begin,$page_size";
                $result = $db->query($sql);
                while($board=$result->fetch_array(MYSQLI_ASSOC)){
                    $title = $board['title'];
                    // 만약 title의 길이가 길면 중간을 '....'으로 표시
                    if(strlen($title)>15){ 
                        $title=str_replace($board["title"],mb_substr($board["title"],0,15,"UTF8")."...",$board["title"]); //title이 15을 넘어서면 ...표시
                    }
                ?>
                <tbody>
                    <tr>
                        <td><?php echo $board['idx'];?></td>
                        <!--제목 클릭시 게시글 읽기-->
                        <td><a href ="/test/notice_board/read?idx=<?php echo $board['idx']; ?>"><?php echo $title;?></td>
                        <td><?php echo $board['author'];?></td>
                        <td><?php echo $board['date'];?></td>
                        <td><?php echo $board['views'];?></td>
                    </tr>
                </tbody>
        <?php   } ?>    <!--while문 종료-->
            </table>
            <hr/>
            <a class="btn btn-outline-primary" href="/test/notice_board/write">글쓰기</a>
            <a class="btn btn-outline-primary" href="/test/login/logout">로그아웃</a>
        
        <!--페이지 번호-->
        <?php
            $page_sql = "SELECT COUNT(idx) FROM board";     // 글 목록 수 구하는 쿼리문
            $page_result = mysqli_query($db$page_sql);    // 쿼리문 실행
            $row = mysqli_fetch_row($page_result);          // 실행 값 열로 가져오기(데이터 접근하기 위해)
            $total_records = $row[0];                       // [0]번째에 저장되어 있으므로..
            $total_pages = ceil($total_records / $page_size);   // 페이지 수 구하기
        ?>
            <div class = "pagination justify-content-center">
            <ul class = 'pagination'>
                <?php
                    // 페이지 수만큼 페이지 버튼 만들고 해당 페이지 번호에 게시글 배정
                    for ($i=1$i<=$total_pages$i++) {
                        ?><li class='page-item'><a class='page-link' href='/test/main?current_page=<?php echo $i;?>'><?php echo $i;?></a></li>
                <?php } ?>
            </div>
        </div>
    </body>
</html>
 

실행 화면

3페이지로 이동

 

+ Recent posts