본문 바로가기

웹 모의해킹 스터디/웹 개발 (PHP | MySQL)

[php/mysql] 게시판 리스트 구현하기 (메인 페이지)

 

페이징 처리는 하지 않고 게시글만 역순으로 출력되게 하였다.

 

[index.php]

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

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="../css/index.css" />
    <title>메인페이지</title>
</head>

<body>
    <?php
    include "../db_conn.php";

    session_start();
    $name = isset($_SESSION['name']) ? $_SESSION['name'] : "";
    // echo $name;
    if (!$name) {
        header("location:../member/login.php");
        exit;
    }
    $sql = "select * from board order by regdate desc";
    $result = mysqli_query($db_conn, $sql);
    $num = mysqli_num_rows($result);
    ?>

    <p style="font-size:30px; text-align:center"><a href="index.php"><b style="color: black;">게시판</b></a></p>
    <div style="text-align:center;">
        <b><?php echo $_SESSION['name'] ?></b>님, 환영합니다.
        <button class="btn" onclick="location.href='../mypage/mychange.php?user=<?= $_SESSION['id'] ?>'">내 정보</button>
        <button class="btn" onclick="location.href='../member/logout.php'">로그아웃</button>
    </div>
    <hr>
    <br>

    <div>
        <table align="center">
            <thead align="center">
                <tr>
                    <td class="htd" width="100" align="center">작성자</td>
                    <td class="htd" width="400" align="center">제목</td>
                    <td class="htd" width="50" align="center">조회수</td>
                    <td class="htd" width="200" align="center"></td>
                </tr>
            </thead>
            <tbody>
                <?php
                while ($rows = mysqli_fetch_assoc($result)) {
                ?>
                    <tr>
                        <td width="50" align="center"><?php echo $rows['writer']  ?></td>
                        <td width="400" align="center">
                            <a href="../board/read.php?idx=<?php echo $rows['idx'] ?>">
                                <?php echo $rows['title'] ?></a>
                        </td>
                        <td width="50" align="center"><?php echo $rows['views'] ?></td>
                        <td width="200" align="center"><?php echo $rows['regdate'] ?></td>
                    </tr>
                <?php
                };
                ?>
            </tbody>
        </table>
    </div>

    <div style="text-align:center;" class="write">
        <button type="button" onclick="location.href='../board/write.php';">글쓰기</button>
    </div>
</body>
</html>

 

 

 

[index.css]

.btn {
  border: none;
  background-color: lightgray;
  border-radius: 5px;
  height: 25px;
  color: black;
}

.column {
  margin-bottom: 300px;
}

.search {
  text-align: center;
  margin-bottom: 30px;
}

table {
  /* border-top: 1px solid #444444; */

  border-collapse: collapse;
  width: 1200px;
  margin-bottom: 30px;
}

tr {
  border-bottom: 1px solid #444444;

  padding: 10px;
}

td {
  border-bottom: 1px solid #efefef;

  padding: 10px;
}

.htd {
  font-weight: bold;
  font-size: 18px;
  border-bottom: black;
}

#search_opt,
.textform {
  border: 1px solid #d9d9d9;
  border-radius: 5px;
  width: 300px;
  height: 30px;
}

.submit {
  width: 60px;
  height: 33px;
  border-radius: 5px;
  border: 1px solid gray;
  background: white;
  cursor: pointer;
}

.write {
  text-align: center;
  margin-bottom: 30px;
}

.write button {
  width: 60px;
  height: 40px;
  border-radius: 5px;
  background: black;
  color: white;
  font-weight: bold;
  cursor: pointer;
}

a:link {
  color: #57a0ee;
  text-decoration: none;
}

.paging {
  text-align: center;
}

.paging a {
  color: black;
  font-weight: bold;
  font-size: 15px;
}

.paging a:hover {
  color: gray;
}

 

 

 

메인 페이지 화면