PHP와 Xampp를 연동하여 간단한 게시판을 만들려 합니다

 

일단 Xampp를 설치 후  apache와 mysql을 실행시켜 줍니다.

 

그리고 xammp -> htdocs에 폴더를 하나 만들어 주고 그 폴더를 편집기에서 열어줍니다. 

 

저는 vs code를 사용했습니다.

 

데이터베이스를 연결해주는 역할을 하는 db.php 파일 먼저 생성합니다.

 

<?php
	session_start();
	header('Content-Type: text/html; charset=utf-8'); 

	
	$db = new mysqli("localhost","root","root","first"); 
	$db->set_charset("utf8");

	function mk($sql)
	{
		global $db;
		return $db->query($sql);
	}
?>

new sqli("주소","DB 아이디","DB 비번","DB 이름")으로 적어주시면 됩니다.

 

다음으로는 기본이 되는 index.php와 style.css를 작성해보겠습니다.

 

<?php include "db.php"; ?>
<!doctype html>
<head>
<meta charset="UTF-8">
<title>게시판</title>
<link rel="stylesheet" type="text/css" href="/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>
            </tr>
        </thead>
        <?php
        
          $sql = mk("select * from board order by idx desc limit 0,5"); 
            while($board = $sql->fetch_array())
            {
              
              $title=$board["title"]; 
              if(strlen($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>
        </tr>
      </tbody>
      <?php } ?>
    </table>
    <div id="write_btn">
      <a href="/page/board/write.php"><button>글쓰기</button></a>
    </div>
  </div>
</body>
</html>
@charset "utf-8";

/* 전체 옵션 */
* {
	margin: 0 auto;
	padding: 0;
	font-family: 'Malgun gothic','Sans-Serif','Arial';
}
a {
	text-decoration: none;
	color:#333;
}
ul li {
	list-style:none;
}

/* 공통 */
.fl {
	float:left;
}
.tc {
	text-align:center;
}

/* 게시판 목록 */
#board_area {
	width: 900px;
	position: relative;
}
.list-table {
	margin-top: 40px;
}
.list-table thead th{
	height:40px;
	border-top:2px solid #09C;
	border-bottom:1px solid #CCC;
	font-weight: bold;
	font-size: 17px;
}
.list-table tbody td{
	text-align:center;
	padding:10px 0;
	border-bottom:1px solid #CCC; height:20px;
	font-size: 14px 
}
#write_btn {
	position: absolute;
	margin-top:20px;
	right: 0;
}

이렇게 작성하셨다면 저장하시고 인터넷창을 열어서 localhost/파일명/index.php로 들어가면

 

다음 포스팅에는 글쓰기 기능을 구현해보겠습니다.

반응형
복사했습니다!