페이지네이션 & 게시글보기

페이지네이션(Pagenation)

게시판에 게시글 목록 생성

  1. createBoardData.php

<?php
    include "../connect/connect.php";
    include "../connect/session.php";

    for( $i=1; $i<=100; $i++ ){
        $regTime = time();    
        
        $sql = "INSERT INTO studyBoard(studyMemberID, boardTitle, boardContent, boardView, regTime) VALUES('2', '게시판 제목입니다.${i}', '게시글 ${i}번째 내용입니다.', '1', '$regTime')";

        $result = $connect -> query($sql);

        if( $result ){
            echo "{$i} data input true";
        } else {
            echo "{$i} data input false";
        }
    }
?>

페이지네이션을 위한 php 코드

2. board.php 코드 수정

<?php

    // $page = $_GET['page'];
    // echo $page;

    if(isset($_GET['page'])){
        $page = (int) $_GET['page'];
    } else {
        $page = 1;
    }

    $numView = 10;
    $viewLimit = ($numView * $page) - $numView;

    //1~20   : LIMIT 0,  20 ---> $page = 1    ($numView * $page) - $numView
    //21~40  : LIMIT 20, 20 ---> $page = 2    ($numView * $page) - $numView
    //41~60  : LIMIT 40, 20 ---> $page = 3    ($numView * $page) - $numView
    //61~80  : LIMIT 60, 20 ---> $page = 4    ($numView * $page) - $numView
    //81~100 : LIMIT 80, 20 ---> $page = 5    ($numView * $page) - $numView



    
    $sql = "SELECT b.studyBoardID, b.boardTitle, m.youName, b.boardView, b.regTime FROM studyBoard b JOIN studyMember m ON (m.studyMemberID = b.studyMemberID) ORDER BY studyBoardID DESC LIMIT {$viewLimit}, {$numView}";
    $result = $connect -> query($sql);

    if($result){
        $count = $result -> num_rows;

        if($count > 0){
            for($i=1; $i<=$count; $i++){
                $boardInfo = $result -> fetch_array(MYSQLI_ASSOC);
                echo "<tr>";
                echo "<td>".$boardInfo['studyBoardID']."</td>";
                echo "<td><a href='boardView.php?boardID={$boardInfo['studyBoardID']}'>".$boardInfo['boardTitle']."</a></td>";
                echo "<td>".$boardInfo['youName']."</td>";
                echo "<td>".date('Y-m-d', $boardInfo['regTime'])."</td>";
                echo "<td>".$boardInfo['boardView']."</td>";
                echo "</tr>";
            }
        }
    }
?>

생성한 게시글의 데이터 보여주기

게시글 번호 / 제목 / 작성자 / 등록일 / 조회수 / 내용

3. boardView.php

<?php
    $boardID = $_GET['boardID'];
    
    // echo $boardID;

    $sql = "SELECT b.boardTitle, b.boardContent, b.boardView, m.youName, b.regTime From studyBoard b JOIN studyMember m ON (b.studyMemberID = m.studyMemberID) WHERE b.studyBoardID = {$boardID}";
    $result = $connect -> query($sql); 

    $view = "UPDATE studyBoard SET boardView = boardView + 1 WHERE studyBoardID = {$boardID}";
    $connect -> query($view);

    if( $result ){
        $info = $result -> fetch_array(MYSQLI_ASSOC);
    
        echo "<tr><th>제목</th><td class='left'>".$info['boardTitle']."</td></tr>";
        echo "<tr><th>글쓴이</th><td class='left'>".$info['youName']."</td></tr>";
        echo "<tr><th>등록일</th><td class='left'>".date('Y-m-d H:i', $info['regTime'])."</td></tr>";
        echo "<tr><th>조회수</th><td class='left'>".$info['boardView']."</td></tr>";
        echo "<tr><th>내용</th><td class='left height'>".nl2br($info['boardContent'])."</td></tr>";
    }
?>

nl2br : 문자열 내의 줄바꿈 기호를 HTML 태그의 <br>로 바꾸어 주는 PHP 함수

Last updated