본문 바로가기
따라 공부하기/PHP게시판 따라하기

[PHP+MySQL]게시판 만들기 2 - 글list 출력

by DawIT 2020. 12. 1.
320x100

이글은 이곳을 참고하여 쓰여졌습니다.

 

이번엔 index.php를 제작함으로써 메인 페이지를 만들 것이다.

 

그런데 기존 코드를 그대로 따라하면 얻는게 하나도 없으니 '추천수' 기능을 추가하기로 했다.

 

일단 기존 board 테이블에 thumbup 컬럼을 추가해준다.

 

 

thumbup 컬럼은 int type 이고, not null, default = 0 을 속성으로 가진다.

 

<?php include  $_SERVER['DOCUMENT_ROOT']."/BBS/db.php"; ?>
<!doctype html>
<head>
<meta charset="UTF-8">
<title>게시판</title>
<link rel="stylesheet" type="text/css" href="/BBS/css/style.css" />
</head>
<body>
<div id="board_area"> 
  <h1>자유게시판</h1>
  <h4>자유롭게 글을 쓸 수 있는 게시판입니다.</h4>
    <table class="list-table">
      <thead>
          <tr>
              <th width="70">번호</th>
                <th width="500">제목</th>
                <th width="120">글쓴이</th>
                <th width="100">작성일</th>
                <!-- 추천수 항목 추가 -->
                <th width="100">추천수</th>
                <th width="100">조회수</th>
            </tr>
        </thead>
        <?php
        // board테이블에서 idx를 기준으로 내림차순해서 10개까지 표시
          $sql = query("select * from board order by idx desc limit 0,10"); 
            while($board = $sql->fetch_array())
            {
              //title변수에 DB에서 가져온 title을 선택
              $title=$board["title"]; 
              if(strlen($title)>30)
              { 
                //title이 30을 넘어서면 ...표시
                $title=str_replace($board["title"],mb_substr($board["title"],0,30,"utf-8")."...",$board["title"]);
              }
        ?>
      <tbody>
        <tr>
          <td width="70"><?php echo $board['idx']; ?></td>
          <td width="500"><a href=""><?php echo $title;?></a></td>
          <td width="120"><?php echo $board['name']?></td>
          <td width="100"><?php echo $board['date']?></td>
          <td width="100"><?php echo $board['hit']; ?></td>
          <!-- 추천수 표시해주기 위해 추가한 부분 -->
          <td width="100"><?php echo $board['thumbup']?></td>
        </tr>
      </tbody>
      <?php } ?>
    </table>
    <div id="write_btn">
      <a href="/page/board/write.php"><button>글쓰기</button></a>
    </div>
  </div>
</body>
</html>

 

index.php이다. 기존의 코드에서 추천수 항목을 추가했다. 그리고 원래 페이지당 글 출력 개수가 5개였는데 10개로 바꿨다.

 

이렇게 코드를 만들고 페이지를 새고고침하면 다음과 같다.

 

 

페이지가 정상적으로 출력되기는 하지만, 글이 하나도 없어서 일단 WorkBench에 들어가서 test용 row 를 만들어줬다.

 

INSERT INTO `bbs`.`board` (`name`, `pw`, `title`, `content`, `date`, `hit`)
 VALUES ('test', '1111', 'testtitle', 'testcontent', '2020-12-01', '0');

 

이제 다시 페이지를 새로고침 해보면,

 

 

이렇게 정상적으로 출력되는것을 확인할 수 있다.

 

10개까지만 출력되는것도 확인해야 하니 row를 더 만들었다.

 

이렇게 완~전 대충 row들을 만들고 다시 새로고침 해봤다.

 

 

가장 최근 글 부터 10개가 잘 출력되는 것을 확인할 수 있다.

 

그러나 현재 출력을 제외하고는 아무 기능이 없다.

 

그래서 다음은 글쓰기 기능을 구현하려고 한다.

댓글