1. freeboard.php
<html><head>
<title>자유게시판</title>
</head>
<body>
<h3><p align=center>자유게시판</p></h3>
<table width=800 align=center>
<tr bgcolor=cyan>
<td align=center width=80>번호</td> // 상단 title
<td align=center width=420>제목</td>
<td align=center width=100>작성자</td>
<td align=center width=130>작성일</td>
<td align=center width=70>조회수</td>
</tr>
<?php
include "db_connect.php"; // GET 으로 전달된 값 입력
$page = $_GET[page]; // 게시글 리스트가 모두 몇 페이지 되는지
$num_records_per_page = 5; // 한 페이지에 표시될 레코드 수 설정
$sql = "select count(*) from freeboard"; // 전체 레코드 수 알아내기
$result = mysql_query($sql, $connect); // sql 연결
$num_records = mysql_result($result,0 ,0); //
$num_pages = ceil($num_records / $num_records_per_page); // 전체 페이지 수 구하기 ceil
// 현재 페이지의 첫 번째 레코드 계산
$page = min(max(1, $page), $num_pages); // 1- 출력 할 페이지 번호, $num_pages 전체 페이지 수
$start = ($page - 1) * $num_records_per_page; // 출력을 시작 할 첫 번째 레코드 위치, 현재 페이지의 레코드들 읽기
$sql = "select * from freeboard order by num desc"; // freeboard에서 num 기준으로 내림차순으로 정렬
$sql .= "limit $start, $num_records_per_page";
$result = mysql_query($sql, $connect);
while ($row = mysql_fetch_array($result)) { // 게시글 리스트 출력
echo "<tr>
<td align=center>$row[num]</td> // 번호
<td><a href='view.php?num=$row[num]$page=$page'>$row[title]</a></td>
// 제목, 누르면 wiew.php로 들어가지며 내용을 보고 수정 및 삭제 가능
<td align=center>$row[name]</td> // 작성자
<td align=center>$row[register_day]</td> // 작성일
<td align=center>$row[hits]</td> // 조회수
</tr>
}
mysql_close($connect);
?>
<tr><td colspan=5 height=20></td></tr>
<tr><td colspan=5 align=center>
<?php // 한 화면에 표시 할 페이지 번호 링크의 시작과 끝 번호를 계산.
$num_links_per_view = 3; // 한 화면에 표시될 페이지 번호의 수
$block = ceil($page / $num_links_per_view); // 현재 화면에 표시 할 링크 블럭 번호, ceil() 올림 함수
$first_link = ($block -1) * $num_links_per_view +1; // 첫 번째 링크 번호
$last_link = min($first_link + $num_links_per_view - 1, $num_pages); // 마지막 링크 번호
if ($first_link !=1)
echo "<a href='freeboard.php?page=" . ($page - $num_links_per_view) . "'>[< 이전]</a> $nbsp"; // 페이지 번호들 출력
for ($i= $first_link; $i <= $last_link; $i++) { // first_link 부터 last_link 까지
if($page == $i)
echo "<b>[$i]</b>  "; //   띄어쓰기
else
echo "<a href='freeboard.php?page=$i'>[$i]</a> ";
}
if ($last_link != $num_pages)
echo "<a href='freeboard.php?page=".($page + $num_links_per_view) . "'>[다음 >]</a>  ";
?>
</td></tr>
<tr>
<td colsapn=5 align=right><input type=button value=글쓰기 onclick="location.href='write_form.php?page=<?=$page?>'"></td>
</tr>
</table>
</body>
</html>
2. db_connect.php
<?php
$connect = mysql_connect("localhost","root","rootoor");
if (!$connect)
die ("DB 접속 실패 :".mysql_error());
mysql_select_db("centos_db",$connect);
?>
3. modify.php
<?
include "db_connect.php"
$num = $_GET[num];
$page = $_GET[page];
$name = $_POST[name];
$passwd = $_POST[passwd];
$title = $_POST[title];
$content = $_POST[content];
$sql = "select * from freeboard where num = $num and passwd = '$passwd'";
// 지정된 번호와 비밀번호를 가진 레코드를 읽어 옴 (비밀번호 확인)
$result = mysql_query($sql, $connect);
if (mysql_num_rows($result)) // 번호, 비밀번호가 맞는 레코드가 있다면,
{ // 작성일, 작성자 IP 다시 얻어 옴.
$register_day = date("Y-m-d");
$user_ip = $_SERVER[REMOTE_ADDR]; // 글 내용 업데이트
$sql = "update freeboard set name='$name', title='$title',";
$sql .= "content='$content', register_day='$register_day',";
mysql_query($sql, $connect);
}
else
echo "<script>
alert('비밀번호가 틀렸습니다.');
history.back();
</script>";
mysql_close();
header("Loaction:freeboard.php?page=$page"); // main page로 돌아감
?>
4. modify_form.php
<?php
include "db_connect.php"
$num = $_GET[num];
$page = $_GET[page];
$sql = "select * from freeboard where num=$num";
$result = mysql_query($sql, $connect);
$row = mysql_fetch_arrary($result); // fetch 가져오다.
$row[title] = str_replace(" "," ",$row[title]);
$row[content] = str_replace(" ", " ",$row[content]);
mysql_close();
?>
<html><head>
<title>자유게시판</title>
</head>
<body>
<h3><p align=center>자유게시판</p></h3>
<form method=post action='modify.php?num=<?=$num?>&page=<?=$page?>'>
<table>
5. view.php
<?php
include "db_connect.php";
$num = $_GET[num];
$page = $_GET[page]; // 지정된 번호의 글 읽기
$sql = "select * from freeboard where num=$num";
$result = mysql_query($sql, $connect);
$row = mysql_fetch_array($result); // 제목의 공백, 본문의 공백과 줄 넘김이 웹에서 보이도록 처리
$row[title] = str_replace(" "," ",$row[title]);
$row[content] = str_replace(" "," ",$row[content]);
$row[content] = str_replace("\n","<br>",$row[content]);
$sql = "update freeboard SET hits = hits+1 where num=$row[num]"; // 조회 할 때마다, 조회 수 1 증가.
mysql_query($sql, $connect);
mysql_close();
?>
<html><head>
<title>자유게시판</title>
</head>
<body>
<h3><p align=center>자유게시판</p></h3>
<table width=800 align=center>
<tr><td bgcolor=cyan width=70>제목</td>
<td><? echo $row[title]; ?></td> </tr>
<tr><td bgcolor=cyan>작성자</td>
<td><? echo $row[name]; ?></td> </tr>
<tr><td bgcolor=cyan>IP</td>
<td><? echo $row[user_ip];?></td> </tr>
<tr><td bgcolor=cyan>내용</td>
<td><? echo $row[content]; ?></td> </tr>
<tr><td></td>
<td><input type=button value=수정
onclick="location.href='modify_form.php?num=<?=$num?> &page=<?=$page?>'">
<input type=button value=삭제
onclick="location.href='passwd_form.php?num=<?=$num?> &page=<?=$page?>'">
<input type=button value=목록보기
onclick="location.href='freeboard.php?num=<?=$num?> &page=<?=$page?>'">
</table>
</body></html>
6. write.php
<?php
inclue "db_connect.php";
$name = $_POST[name];
$passwd = $_POST[passwd];
$title = $_POST[title];
$content = $_POST[content];
if ($name && $passwd && $title && $content) { // 작성일, 작성자 IP 얻기
$register_day = date("Y-m-d");
$user_ip = $_SERVER[REMOVE_ADDR]; // 이 부분도 $_GET[] 처럼 정보를 추출
// 쿼리 실행
$sql = "insert into freeboard";
$sql .= "(name, passwd, title, content, register_day, hits, user_ip)";
$sql .= "values('$name','$passwd','$title','$content',";
$sql .= "'$register_day', 0, '$user_ip')";
mysql_query($sql, $connect);
mysql_close();
header("Location:freeboard.php");
}
else
echo "<script>
alert('이름, 비밀번호, 제목, 내용이 모두 입력되어야 합니다.');
history.back(); // history.go(-1); 도 가능
</script>";
?>
7. write_form.php
<html><head>
<title>자유게시판</title>
</head><body>
<h3><p align=center>자유게시판</p></h3>
<form method=post action=write.php>
<table>
<tr><td bgcolor=cyan>이름</td>
<td align=left><input type=text name=name size=25 maxlength=16 value=''></td></tr>
<tr><td bgcolor=cyan>비밀번호</td>
<td align=left><input type=password name=passwd size=25 maxlength=16 value=''></td></tr>
<tr><td bgcolor=cyan>제목</td>
<td align=left><input type=text name=title size=50 maxlength=100 value=''></td></tr>
<tr><td bgcolor=cyan>내용</td>
<td align=left><textarea name=content cols=74 rows=14 wrap=virtual></textarea></td></tr>
<tr><td><input type=submit value=글등록></td>
<td><input type=button value=목록보기 onclick="location.href='freeboard.php?page=<?=$_GET[page]?>'"></td></tr>
</table>
</form>
</body></html>
1. freeboard.php // 첫 화면, main_page
html h3, title, db_connect, page 수, 게시글 내림차순 출력 view.php, 게시글 페이지로 나눔, 글쓰기write_form.php
2. db_connect.php // sql connect
mysql 연결 모든 파일에 첫번째로 들어가서 연결
3. modify.php // 수정
수정파일, $num값과 $passwd 값 일치 확인, sql update $name, $title, $content, $register_day변경
4. modify_form.php
modify.php 연결 modify.php에서 sql 안에 변경된 data값을 화면에 불러온다
5. view.php
조회수 1증가, html title, modify_form.php 수정 passwd_form.php 삭제 freeboard.php 목록보기 연결
6. write.php
name, passwd, title, content, register_day, hits, user_ip 값 입력 sql 에 저장 저장 후, 전 페이지로 돌아감
7. write_form.php
글쓰기 페이지 크기 모양 잡고, write.php 연결해서 sql에 저장된 값을 화면에 출력
PHP Cookie & Session (0) | 2022.06.07 |
---|---|
PHP 주소록 만들기 (0) | 2022.06.07 |
PHP Login(COOKIE) (0) | 2022.06.07 |
PHP 반복문 조건문 (0) | 2022.06.07 |
PHP 연관배열(Associate array) (0) | 2022.06.07 |