Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 주소검색기능구현
- mysqli_fetch_row
- 파일 업로드
- 로그아웃
- 게시판 구현
- LOS 1
- Lord of SQLinjection 1
- LOS 2
- 검색 기능
- LOS 3
- PHP회원가입
- PHP로그아웃
- php #쓰기 #읽기 #수정 #삭제 #게시판
- 다운로드 구현
- gremlin
- 게시판 리스트
- mysqli_fetch_assoc
- 게시판 검색 기능 구현
- php주소검색기능구현
- php
- 로그인 기능
- Lord of SQLinjection 3
- 우편주소DB
- 간단한 로그인 구현
- Lord of SQLinjection 2
- 우편주소검색
- cobolt
- php 다운로드
- mysqli_fetch_array
- Goblin
Archives
- Today
- Total
ME
[PHP] 게시판 리스트 구현 본문
완성
1. DB 테이블 생성
CREATE TABLE board (
idx int AUTO_INCREMENT,
title varchar(20),
name varchar(20),
memo text,
date varchar(20),
PRIMARY KEY(idx)
);
위 SQL 문을 입력해 테이블을 생성해주자.
mysql> show columns from board;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| idx | int | NO | PRI | NULL | auto_increment |
| title | varchar(20) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| memo | text | YES | | NULL | |
| date | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
2. 게시판 폼 생성
<?php
include '../db.php';
?>
<!DOCTYPE html>
<html lang="ko">
<head>
<title>board</title>
</head>
<body>
<h2>게시판</h2>
<table width=800 border="1">
<thead>
<tr align=center>
<th width=50>번호</th>
<th>제목</th>
<th width=100>작성자</th>
<th width=90>작성일</th>
</tr>
</thead>
</table>
</body>
</html>
3. 게시판 리스트 출력
<?php
$q = "SELECT * FROM board ORDER BY idx DESC";
$d = mysqli_query($conn, $q)
while ($row = mysqli_fetch_array($d)) { ?>
<tr>
<td> <?=$row['idx']?> </td>
<td> <a href="read.php?idx=<?=$row['idx']?>"><?=$row['title']?></a></td>
<td> <?=$row['name']?> </td>
<td> <?=substr($row['date'],0,10) ?> </td>
</tr>
<?php } ?>
$q = "SELECT * FROM board order by idx DESC";
ORDER BY 컬럼 DESC 명령어로 내림차순 정렬을 해줘서 최신글을 불러올 수 있게 해준다.
while ($row = mysqli_fetch_array($d)) { ?>
<tr>
<td> <?=$row['idx']?> </td>
<td> <a href="read.php?idx=<?=$row['idx']?>"><?=$row['title']?></a></td>
<td> <?=$row['name']?> </td>
<td> <?=substr($row['date'],0,10) ?> </td>
</tr>
<?php } ?>
DB에서 실행한 SQL문의 결과가 있으면 전부 가져올 때까지 반복한다.
<td> <a href="read.php?idx=<?=$row['idx']?>"><?=$row['title']?></a></td>
a 태그로 제목에 링크를 달아주고,
제목을 클릭하면 read.php?idx= 번호 로 이동한다.
제목을 클릭했을 때 read.php 뒤에 idx=5가 붙은걸 확인할 수 있다.
list.php (전체 코드)
<?php
include '../db.php';
?>
<!DOCTYPE html>
<html lang="ko">
<head>
<title>board</title>
</head>
<body>
<h2>게시판</h2>
<table width=800 border="1">
<thead>
<tr align=center>
<th width=50>번호</th>
<th>제목</th>
<th width=100>작성자</th>
<th width=90>작성일</th>
</tr>
</thead>
<?php
$q = "SELECT * FROM board ORDER BY idx DESC";
$d = mysqli_query($conn, $q);
while ($row = mysqli_fetch_array($d)) { ?>
<tr>
<td> <?=$row['idx']?> </td>
<td> <a href="read.php?idx=<?=$row['idx']?>"><?=$row['title']?></a></td>
<td> <?=$row['name']?> </td>
<td> <?=substr($row['date'],0,10) ?> </td>
</tr>
<?php } ?>
</table>
<a href="write.php">글쓰기</a>
</body>
</html>
'웹개발 > PHP' 카테고리의 다른 글
[PHP] 게시판 검색 기능 구현 (0) | 2023.01.01 |
---|---|
[PHP] 게시판 작성, 읽기, 수정, 삭제 구현 (0) | 2022.12.29 |
주소 검색 구현 [1] - 우편주소 DB 저장 (0) | 2022.12.12 |
[PHP] 회원가입, 로그아웃 기능 구현 (0) | 2022.10.26 |
mysqli_fetch_array, row, assoc 차이점 (0) | 2022.10.23 |
Comments