강좌
클라우드/리눅스에 관한 강좌입니다.
프로그램 분류

는. 좀 더 깔끔하게 다듬기 -1 화면을 더 이쁘게.

작성자 정보

  • 웹관리자 작성
  • 작성일

컨텐츠 정보

본문

[강좌] php+mysql 게시판 만들기 #는-1

안녕하세요. 디망쉬입니다.

벌써 개발과 관련된 대부분의 내용이 나왔군요. 앞으로 관련글 기능 구현이라는 마지막 높은(?) 관문이 남았지만
여기까지 온 것만 해도 대단한 겁니다. ^^ 자 수고 수고!

강좌가 한 70% 정도 진행됨에 따라 다음에 제가 진행할 강좌를 미리 예고해드리죠. (제 예고는 언제나처럼 저
자신에게 미리 일거리를 만들고 이렇게 하자! 의 의미가 강합니다 ^^;)

1. CrazyWebBoard 1.01 소스 분석 (CGI)

2. CrazyWebBoard 3.01 업그레이드 하기 (CGI)

3. 어떻게 공부 시작할까요? (웹)

4. 게임 기획 (게임)

정도입니다. 여기서 본 강좌를 보시는 분들이라면 위의 1~3 정도만 접하게 되겠군요. :] 차후에 기회가
닿으면 HTML 게임 만드는 강좌도 쓰고 싶지만 이건 정말 소스보다 설명이 훨씬 많은 강좌가 될 가능성이
농후해서 연재할 가능성은 미비하내요. ^^;

아참. 그리고 2000년 7월 4일 이후로 제 강좌의 게재 가능한 곳에서 나우누리 웹 디자인 동(go webd)를
제외합니다. 이유는 간단. 아무리 생각해봐도 이 강좌는 웹 디자인과 무관하거든요. ^^; (제법 도배했는데
한 번에 다 지우니 뭔가 찝찝하내요 죄송 흑)

아참. '쉬-짠' 보면 일부 문서는 끝이 짤린 거 같습니다. 특히 '쉬-1' 과 '쉬-2'.

'쉬-짠'은 필요한 부분 찾아가며 보는 觀隙繭?일부러 잡담이나 그런 거 안쓴 겁니다. 그런데
'쉬-1' 과 '쉬-2' 는 마치 중간에 잘린 것처럼 나오는데 그건 문서 파일을 업로드하는데 괄호
하나와 마침표가 같이 업로드 되지 않아서 그런 거랍니다. 문서 자체는 정상입니다. ^^;

자. 각설하고 이제 게시판을 꾸며볼까요?


icon04.gif 는. 화면을 좀 더 이쁘게

a. 글 입력 하기


이전 강좌에서 우린 게시판을 기능 별로 파일 단위로 자른다고 했었습니다. :) 그런데 게시물 입력 폼은 fill.php3 였죠. 이제 fill.php3 를 클릭하면 글 쓰기 입력 폼이 나옵니다.

일단 글 쓰기 입력 폼을 디자인 해볼까요?

<?
/*--------------------------
filename : fill.php3
--------------------------*/

echo ("

<html>

<head>
<title>글 입력</title>
</head>

<body>
<center>
광고 및 욕설 글은 언제나 발견되는 대로 삭제됩니다. ^^<p>

<form method="post" action="run.php3"
enctype="multipart/form-data">
<input type="hidden" name="board"
value="$board">
<input type="hidden" name="mode" value="write">

<table width="550" cellspacing="1"
border="0" cellpadding="2">
<tr>
<td align="left"><font size="2"><b>
| <a href="list.php3?board=$board"> |
게시물 목록</a>
</b></font></td>
</tr>
</table>

<table width="400" cellspacing="1" bgcolor="#A5A595"
border="0" cellpadding="5">
<!-- 작성자명 -->
<tr>
<td width="30%" bgcolor="#A5A595" align="center">
<font size="2" color="white"><b>작성자명</b></font></td>
<td width="70%" bgcolor="white" align="left">
<font color="white">
<input type="text" name="fil[name]" value="$mwbwd[name]"
size="10" maxlength="10">
</font></td>
</tr>

<!-- Email -->
<tr>
<td width="30%" bgcolor="#A5A595" align="center">
<font size="2" color="white"><b>E-mail</b></font></td>
<td width="70%" bgcolor="white" align="left">
<font color="white">
<input type="text" name="fil[email]" value="$mwbwd[email]"
size="20" maxlength="256">
</font></td>
</tr>

<!-- Homepage -->
<tr>
<td width="30%" bgcolor="#A5A595" align="center">
<font size="2" color="white"><b>Homepage</b></font></td>
<td width="70%" bgcolor="white" align="left">
<font color="white">
<input type="text" name="fil[homepage]" value="$mwbwd[homepage]"
size="20" maxlength="256">
</font></td>
</tr>

<!-- Password -->
<tr>
<td width="30%" bgcolor="#A5A595" align="center">
<font size="2" color="white"><b>비밀번호</b></font></td>
<td width="70%" bgcolor="white" align="left">
<font color="white">
<input type="password" name="fil[pw]" value="$mwbwd[pw]"
size="10" maxlength="20">
</font></td>
</tr>

<!-- Subject -->
<tr>
<td width="30%" bgcolor="#A5A595" align="center">
<font size="2" color="white"><b>글 제목</b></font></td>
<td width="70%" bgcolor="white" align="left">
<font color="white">
<input type="text" name="fil[title]"
size="30" maxlength="256">
</font></td>
</tr>

<!-- Text -->
<tr>
<td width="30%" bgcolor="#C5C5B5" align="center">
<font size="2" color="white"><b>본 문</b></font></td>
<td width="70%" bgcolor="white" align="left">
<font color="white">
<textarea name="fil[text]" wrap="hard" rows="10" cols="36"></textarea>
</font></td>
</tr>
</table>
<input type="submit" value="저장하자">
<input type="reset" value="재작성">
</form>

<table width="550" cellspacing="1"
border="0" cellpadding="2">
<tr>
<td align="right"><font size="2"><b>
| <a href="list.php3?board=$board"> |
게시물 목록</a>
</b></font></td>
</tr>
</table>

</center>
</body>

</html>
"); // 전체 화면 출력 완료
?>

이걸로 끝. 이전보다는 이쁠 겁니다. ^^;
여기서 이전 소스에서 약간 수정된 부분이 있습니다.
잘 보시면 입력 태그(input)에서 value="$mwbwd[homepage]" 이런게 보이실 겁니다.
이건 쿠키에 대한 겁니다. 쿠키가 뭔지는 이전에 설명해드렸죠? ^^;

이걸 게시판에 적용하면 편합니다.
글을 한 번 작성하면 작성자명, email, homepage 등을 저장하고 있다가 또 글을 쓰려 하면 자동으로 작성자명, email, homepage 내용이 입력되어 있는거죠. :]

그런 쿠키 변수가 $mwbwd 입니다. :] 이건 run.php3 에서 글 저장할 때 발생시키면 되죠.


b. 글 목록 보기


글 목록 보기에서 어지간한 함수들은 function.php3 에 넣어놨으니 list.php3 만 잘 꾸미면 되겠죠?
하지만 실질적으로 글 리스트를 뿌려주는건 function.php3 입니다.
그래서 관리상 function.php3 과 list.php3 를 살짝 수정해보겠습니다.

일단 function.php3 에서 arti_list 함수에서

while($list = mysql_fetch_array($result)) {
echo "$list[num]-
<a href="view.php3?board=$board&no=$list[no]">
$list[usrtitle]</a>-$list[usrname]<p> ";
}

부분을 지우세요. 그 자리에

return $result;

를 넣으세요. 그러면 아래처럼 됩니다.

function arti_list($tlpn, $perpage) {
$connect = mysql_connect("localhost","root","althsus!");
mysql_select_db("cocoboardDB", $connect);

global $board, $page;

$result = lpp($tlpn, $perpage, $page);

return $result;
}

자. 이제 while 부분을 list.php3 에 넣어 볼까요?

<?
/*--------------------------
filename : list.php3
--------------------------*/
require "function.php3";

$perpage = "10";

if ($page == "") {
$page = 1;
}

$tlpn = get_pagenum();

$result = arti_list($tlpn, $perpage);

while($list = mysql_fetch_array($result)) {
echo "$list[num]-
<a href="view.php3?board=$board&no=$list[no]">
$list[usrtitle]</a>-$list[usrname]<p> ";
}

movepage($page, 2);

echo "<p>전체 페이지수 : $tlpn[page]
전체 글수 : $tlpn[num]";
?>

이제 준비가 됐습니다. 이걸 이쁘게 꾸며 보도록 하겠습니다. ^^; 소스가 생각보다 깁니다. ^^;

<?
/*--------------------------
filename : list.php3
--------------------------*/
require "function.php3";

$perpage = "10";

if ($page == "") {
$page = 1;
}

$tlpn = get_pagenum();

$result = arti_list($tlpn, $perpage);

echo ("
<html>

<head>
<title>글 목록 보기</title>
</head>

<body>

광고 및 욕설 글은 언제나 발견되는 대로 삭제됩니다. ^^<p>
echo "<p>전체 페이지수 : $tlpn[page]
전체 글수 : $tlpn[num]";

<table width="550" cellspacing="0"
border="0" cellpadding="2">
<tr>
<td align="left"><font size="2"><b>
| <a href="fill.php3?board=$board">글 쓰기</a> |
</b></font></td>

<td align="right"><font size="2">
총 글 수 : $tlpn[num] / 총 페이지 수 : $tlpn[page]
</font></td>
</tr>
</table>

<table width="550" cellspacing="1" bgcolor="#A5A595"
border="0" cellpadding="2">
<tr>
<td width="40" bgcolor="#A5A595" align="center">
<font size="2" color="white">번호</font></td>
<td bgcolor="#C5C5B5" align="center">
<font size="2" color="white">글 제목</font></td>
<td width="80" bgcolor="#A5A595" align="center">
<font size="2" color="white">글쓴이</font></td>
<td width="30" bgcolor="#A5A595" align="center">
<font size="2" color="white">hits</font></td>
<td width="50" bgcolor="#A5A595" align="center">
<font size="2" color="white">날짜</font></td>
</tr>
</table>
");

$i = 0;
while($list = mysql_fetch_array($result)) {
if ($i) {
$i = 0;
$bgs = " bgcolor="#E5E5D5"";
}
else {
$i = 1;
$bgs = "";
}

$dates = date("m-d", $list[filluptime]);
echo "
<table width="550" cellspacing="1"
border="0" cellpadding="2"$bgs>
<tr>
<td align="center" width="40">
<font size="2">$list[num]</font></td>
<td>&nbsp;<font size="2">
<a href="view.php3?board=$board&no=$list[no]">
$list[usrtitle]</a></font></td>
<td align="center" width="80">
<font size="2">$list[usrname]</font></td>
<td align="center" width="30">
<font size="2">$list[hit]</font></td>
<td align="center" width="50">
<font size="2">$dates</font></td>
</tr>
</table>

<center>";
}

echo "<font size="1">"; movepage ($page, 2); echo "</font>";

echo "
</center>

</body>

</html>";
?>


자. 생각보다 여러 부분에 손이 갔군요. 따로 손이 간 부분을 살펴보도록 하겠습니다.

$i = 0;
while($list = mysql_fetch_array($result)) {
if ($i) {
$i = 0;
$bgs = " bgcolor="#E5E5D5"";
}
else {
$i = 1;
$bgs = "";
}

이건 게시판을 조금 더 보기 좋게(이쁨이 아닌 편의성) 하기 위함입니다.
게시물 리스트를 출력할 때 한 줄은 바탕색, 한 줄은 우리가 지정한 E5E5D5 색으로 출력되는거죠. 줄 무늬.

일단 $i 를 0으로 해줍니다. 그런 뒤 while 로 루프가 도는 동안 $i 변수를 체크합니다.

if ($i) {

라는 것은 $i 가 참이냐는거죠. 즉 $i 가 0이 아닌 값일 때 만족되는 조건입니다.
만약 $i 가 0 이 아닌 값일 경우 $i 값을 0 으로 바꾸고 $bgs 변수에 bgcolor="#E5E5D5" 를 넣죠.
만약 $i 가 0이라면 $bgs 에는 아무 내용도 들어가지 않고 $i 를 1 로 바꾸지요. 이렇게
하므로서 $i 는 값이 매번 바뀝니다. ^^; 이걸

<table width="550" cellspacing="1"
border="0" cellpadding="2"$bgs>

에서 사용되는 겁니다.

$dates = date("m-d", $list[filluptime]);

이건 뭔지 아실려나? 이전에 설명해드렸는데.
이건 time() 함수로 생성된 시간 정보를 date() 함수를 통해 분리(parse)해주는 함수입니다. ^^;
자세한 건 '쉬-짠'에서 보시거나 글 입력 관련에서 찾아보세용.
있을 겁니다. ^^;

위의 형식은 월-일 (숫자)로 출력하기 위함이죠.

이외 별도의 설명은 필요없군요. 그런데 잘 보면 글 단위로 테이블을 생성해서 출력합니다.
제가 이 강좌를 쓰며 함께 작성되고 있는 게시판에서 글 리스트의 HTML 소스를 보니

<table width="550" cellspacing="1"
border="0" cellpadding="2">
<tr>
<td align="center" width="40">
<font size="2">13</font></td>
<td>&nbsp;<font size="2">
<a href="view.php3?board=testboard&no=13">
에헤헤. 이뻐졌다</a></font></td>
<td align="center" width="80">
<font size="2">디망쉬</font></td>
<td align="center" width="30">
<font size="2">0</font></td>
<td align="center" width="50">
<font size="2">07-08</font></td>
</tr>
</table>

<table width="550" cellspacing="1"
border="0" cellpadding="2" bgcolor="#E5E5D5">
<tr>
<td align="center" width="40">
<font size="2">12</font></td>
<td>&nbsp;<font size="2">
<a href="view.php3?board=testboard&no=12">
수정하자3</a></font></td>
<td align="center" width="80">
<font size="2">디망쉬</font></td>
<td align="center" width="30">
<font size="2">2</font></td>
<td align="center" width="50">
<font size="2">07-04</font></td>
</tr>
</table>

식으로 되더군요. 그러나 화면에 출력되기는

13 에헤헤. 이뻐졌다 디망쉬 0 07-08
12 수정하자3 디망쉬 0 07-08


이구요. 그냥 간단하게 글 마다 테이블 (html tag)를 생성하지 않고 <tr> tag 로 줄만 띄우면 더 소스가 간단해질텐데 저는 왜 이렇게 했을까요?

다시 말하자면 위처럼 출력되지 않고

<tr>
<td align="center" width="40">
<font size="2">13</font></td>
<td>&nbsp;<font size="2">
<a href="view.php3?board=testboard&no=13">
에헤헤. 이뻐졌다</a></font></td>
<td align="center" width="80">
<font size="2">디망쉬</font></td>
<td align="center" width="30">
<font size="2">0</font></td>
<td align="center" width="50">
<font size="2">07-08</font></td>
</tr>

<tr>
<td align="center" width="40">
<font size="2">12</font></td>
<td>&nbsp;<font size="2">
<a href="view.php3?board=testboard&no=12">
수정하자3</a></font></td>
<td align="center" width="80">
<font size="2">디망쉬</font></td>
<td align="center" width="30">
<font size="2">2</font></td>
<td align="center" width="50">
<font size="2">07-04</font></td>
</tr>

하면 조금 더 간단하다는거죠. 그런데 대체 왜 저는 일일히 테이블을 출력했을 까요.

아주 간단합니다.
서버에 매우 많은 트래픽이 발생될 경우 mysql 에서 쿼리 보내고 결과 얻어낸 뒤 이걸 화면에 뿌려주는 것이 느려지죠.
이용자가 많은 사이트의 게시판이 게시물 리스트가 늦게 뜬다거나 하는 것처럼요.
그런데 실제로는 화면에 출력 html 코드가 뿌려지고 있습니다.
출력 안되는 이유는 간단합니다.
html tag 에서 <table> 이 </table> 로 닫히질 않았기 때문이죠. 게시물 리스트의 <table> 이

</table> 로 닫혀야만 게시물 리스트가 그제서야 웹 브라우저에 짠 출력됩니다.
하지만 서버가 버벅대느라 </table> 를 출력하는데 오래 걸리면 </table> 가 나올 때까지 게시물
리스트가 화면에 게시물 리스트 중 어떠한 글도 출력되지 않는다는 거죠.
그래서 글마다 각 각의 테이블로 처리했습니다. 이러면 게시물 리스트를 뿌릴 때 글마다 착 착 착 뜨겠죠. :]


c. 글 내용 읽기

이것도 매우 쉽게 끝날 거 같은 예감이 들죠? ^^;

<?
/*--------------------------
filename : view.php3
--------------------------*/
$connect = mysql_connect("localhost","아이디","비번");
mysql_select_db("사용DB명",$connect);

require "function.php3";

$updok = mysql_query("UPDATE $board SET
hit = hit + 1 WHERE no=$no", $connect);

$result = mysql_query("SELECT * FROM $board
WHERE no=$no", $connect);

$view = mysql_fetch_array($result);

$view[usrname] = htmlspecialchars($view[usrname]);
$view[usremail] = htmlspecialchars($view[usremail]);
$view[usrtitle] = htmlspecialchars($view[usrtitle]);
$view[contents] = htmlspecialchars($view[contents]);
$dates = date ("Y-m-d, h:i:s", $view[filluptime]);

$perpage = 10;
$tlpn = get_pagenum();
$nowpage = get_page($view[idx]);

if ($view[usremail] != "") {
$name = "<a
href="mailto:$view[usremail]">$view[usrname]</a>";
}
else {
$name = "$view[usrname]";
}
if ($view[usrhomepage] != "") {
$homeurl = "<a
href="$view[usrhomepage]"
target="_blank">[Homepage]</a>";
}
else {
$homeurl = "";
}

echo "
<html>

<head>
<title>글 내용 보기</title>
</head>

<body>
<center>


<table width="550" cellspacing="1"
border="0" cellpadding="2">
<tr>
<td align="left"><font size="2"><b>
|<a href="list.php3?board=$board&page=$nowpage">
게시물 목록</a>
|<a href="edit.php3?board=$board&no=$no">
수정</a>
|<a href="delete.php3?board=$board&no=$no">
삭제</a> |
</b></font></td>
</tr>
</table>

<table width="550" cellspacing="1"
border="0" cellpadding="2">
<tr>
<td colspan="2" bgcolor="#A5A595">
<font color="white" size="2">
$view[num] 번 글 : <b>$view[usrtitle]</b>
</font></td>
</tr>

<tr>
<td align="left" bgcolor="#E5E5D5">
<font size="2">조회수 : $view[hit]</font></td>

<td align="right" bgcolor="#E5E5D5">
<font size="2">$name 님이 $homeurl
$dates 에 작성해주셨습니다.</font></td>
</tr>
</table>

<table width="550" cellspacing="1"
border="0" cellpadding="2">
<tr>
<td><pre><font size="2">
$view[contents]
</font></pre>
</td>
</tr>
</table>

<table width="550" cellspacing="1"
border="0" cellpadding="2">
<tr>
<td align="right"><font size="2"><b>
|<a href="list.php3?board=$board&page=$nowpage">
게시물 목록</a>
|<a href="edit.php3?board=$board&no=$no">
수정</a>
|<a href="delete.php3?board=$board&no=$no">
삭제</a> |
</b></font></td>
</tr>
</table>

</center>

</body>

</html>
";
?>

생각보다 길죠? 또 추가된 부분도 있습니다.

if ($view[usremail] != "") {
$name = "<a
href="mailto:$view[usremail]">$view[usrname]</a>";
}
else {
$name = "$view[usrname]";
}
if ($view[usrhomepage] != "") {
$homeurl = "<a
href="$view[usrhomepage]"
target="_blank">[Homepage]</a>";
}
else {
$homeurl = "";
}

일단 이거. 이건 email 주소가 입력되어 있으면 글쓴이의 이름을 email 주소로 링크 걸어주고, 홈페이지 주소가 있으면 [Homepage] 라는 문자열에 홈페이지 주소를 링크시켜 주는 거랍니다. 그 다음 추가된 건

$perpage = 10;
$tlpn = get_pagenum();
$nowpage = get_page($view[idx]);

바로 이거. 일단 위의 두 줄은 $nowpage 를 위해서입니다.

$nowpage = get_page($view[idx]);

여기서 get_page 함수는 이번에 새로 추가한 함수입니다.
이 함수는 글 번호를 통해 이용자가 현재 어느 페이지에 있는지 알아내는 겁니다.
글이 전체 13 개이고 글을 10개 단위로 글 리스트를 출력할 때 2번 글은 2페이지에 위치합니다.

여기서 이용자가 2번 글을 읽고 있을 때 게시판 목록으로 이동하고 싶은데 게시판 목록을 누르니 1페이지로 이동하게 될 겁니다.
이용자가 어느 페이지에 있는지 모르니까요.
그래서 현재 읽고 있는 글을 토대로 이용자가 몇 페이지에 알아내는 게 필요합니다.
이걸 get_page 함수가 하는거죠.

function.php3 에 추가한 get_page 함수를 둘러볼까요?

function get_page($idx) {
global $tlpn, $perpage, $board;

$mresult = $tlpn[num] - $idx;
$get_page = intval($mresult / $perpage) + 1;

return $get_page;
}

으흠. 일단 $idx 변수를 토대로 계산을 하는군요. 전체 글 갯수에서 $idx (글 출력 인덱스 번호)를 뺀 값을 페이지당 출력할 글 갯수로 나눈 값의 정수에 1을 더하는 겁니다.
실제 예를 들어 직접 테스트를 해볼까요?

현재 읽고 있는 글의 $idx 는 2 이고 글 번호는 2번입니다.
전체 글 갯수는 13개이며, 한 페이지당 출력할 글 갯수는 10개입니다.
글 번호 2는 2페이지에 위치하겠죠?
위 계산에서도 2 페이지가 나오는지 보죠.

$mresult = $tlpn[num] - $idx;
11 = 13 - 2;
2 = intval(11 / 10) + 1;

맞죠? :] 그나저나 get_page 함수 위의

$perpage = 10;
$tlpn = get_pagenum();

이거 신경쓰이죠? 한 번 없애보세요. 에러가 뜹니다. ^^; 제가 소스를 제대로 정리하며 작성하지 않은데서 오는 지저분 소스의 불행한 결말입니다. ;_;


d. 글 삭제 하기

움냐아. 글 읽기와 거의 동일합니다. ^^;

<?
/*--------------------------
filename : delete.php3
--------------------------*/
$connect = mysql_connect("localhost","아이디","비번");
mysql_select_db("사용DB명",$connect);

require "function.php3";

$result = mysql_query("SELECT * FROM $board
WHERE no=$no", $connect);

$view = mysql_fetch_array($result);

$view[usrname] = htmlspecialchars($view[usrname]);
$view[usremail] = htmlspecialchars($view[usremail]);
$view[usrtitle] = htmlspecialchars($view[usrtitle]);
$view[contents] = htmlspecialchars($view[contents]);
$dates = date ("Y-m-d, h:i:s", $view[filluptime]);

$perpage = 10;
$tlpn = get_pagenum();
$nowpage = get_page($view[idx]);

if ($view[usremail] != "") {
$name = "<a
href="mailto:$view[usremail]">$view[usrname]</a>";
}
else {
$name = "$view[usrname]";
}
if ($view[usrhomepage] != "") {
$homeurl = "<a
href="$view[usrhomepage]"
target="_blank">[Homepage]</a>";
}
else {
$homeurl = "";
}

echo "
<html>

<head>
<title>글 삭제 보기</title>
</head>

<body>
<center>

<table width="550" cellspacing="1"
border="0" cellpadding="2">
<tr>
<td align="left"><font size="2"><b>
|<a href="list.php3?board=$board&page=$nowpage">
게시물 목록</a>
|<a href="edit.php3?board=$board&no=$no">
수정</a>
|<a href="delete.php3?board=$board&no=$no">
삭제</a> |
</b></font></td>
</tr>
</table>

<form method="post" action="run.php3"
enctype="multipart/form-data">
<input type="hidden" name="board"
value="$board">
<input type="hidden" name="mode" value="remove">
<input type="hidden" name="delno" value="$no">

<table width="550" cellspacing="1"
border="0" cellpadding="2">
<tr>
<td colspan="2" bgcolor="#A5A595">
<font color="white" size="2">
$view[num] 번 글 : <b>$view[usrtitle]</b>
</font></td>
</tr>

<tr>
<td align="left" bgcolor="#E5E5D5">
<font size="2">조회수 : $view[hit]</font></td>

<td align="right" bgcolor="#E5E5D5">
<font size="2">$view[usrname] 님이
$dates 에 작성해주셨습니다.</font></td>
</tr>
</table>

<table width="550" cellspacing="1"
border="0" cellpadding="2">
<tr>
<td><pre><font size="2">
$view[contents]
</font></pre>
</td>
</tr>
</table>

<table width="550" cellspacing="1"
border="0" cellpadding="2" bgcolor="white">
<tr>
<td><font size="2">
비밀 번호 : <input type="password" name="delpw">
<input type="submit" value="삭제">
</font>
</td>
</tr>
</table>

<table width="550" cellspacing="1"
border="0" cellpadding="2">
<tr>
<td align="left"><font size="2"><b>
|<a href="list.php3?board=$board&page=$nowpage">
게시물 목록</a>
|<a href="edit.php3?board=$board&no=$no">
수정</a>
|<a href="delete.php3?board=$board&no=$no">
삭제</a> |
</b></font></td>
</tr>
</table>

<table width="550" cellspacing="1"
border="0" cellpadding="2">
<tr>
<td align="right"><font size="2"><b>
|<a href="list.php3?board=$board&page=$nowpage">
게시물 목록</a>
|<a href="edit.php3?board=$board&no=$no">
수정</a>
|<a href="delete.php3?board=$board&no=$no">
삭제</a> |
</b></font></td>
</tr>
</table>

</center>

</body>

</html>
";
?>

특별히 어려울게 없으므로(이전 delete.php3 와 view.php3 합친 모습이) 글 수정 폼을 수정해 보겠습니다. ^^;


e. 글 수정 하기

냐옹. 이건 글 입력 폼의 것을 가져다 쓰면 됩니다. ^^;

<?
/*--------------------------
filename : edit.php3
--------------------------*/
$connect = mysql_connect("localhost","아이디","비번");
mysql_select_db("사용DB명",$connect);

$result = mysql_query("SELECT * FROM $board
WHERE no=$no", $connect);

$view = mysql_fetch_array($result);

echo ("

<html>

<head>
<title>글 수정 하기</title>
</head>

<body>
<center>

<form method="post" action="run.php3"
enctype="multipart/form-data">
<input type="hidden" name="board"
value="$board">
<input type="hidden" name="mode" value="modify">
<input type="hidden" name="modno" value="$no">

<table width="550" cellspacing="1"
border="0" cellpadding="2">
<tr>
<td align="left"><font size="2"><b>
|<a href="list.php3?board=$board&page=$nowpage">
게시물 목록</a>
|<a href="view.php3?board=$board&no=$no">
읽기</a> |
|<a href="delete.php3?board=$board&no=$no">
삭제</a> |
</b></font></td>
</tr>
</table>

<table width="400" cellspacing="1" bgcolor="#A5A595"
border="0" cellpadding="5">
<!-- 작성자명 -->
<tr>
<td width="30%" bgcolor="#A5A595" align="center">
<font size="2" color="white"><b>작성자명</b></font></td>
<td width="70%" bgcolor="white" align="left">
<font color="white">
<input type="text" name="fil[name]"
value="$view[usrname]"
size="10" maxlength="10">
</font></td>
</tr>

<!-- Email -->
<tr>
<td width="30%" bgcolor="#A5A595" align="center">
<font size="2" color="white"><b>E-mail</b></font></td>
<td width="70%" bgcolor="white" align="left">
<font color="white">
<input type="text" name="fil[email]"
value="$view[usremail]"
size="20" maxlength="256">
</font></td>
</tr>

<!-- Homepage -->
<tr>
<td width="30%" bgcolor="#A5A595" align="center">
<font size="2" color="white"><b>Homepage</b></font></td>
<td width="70%" bgcolor="white" align="left">
<font color="white">
<input type="text" name="fil[homepage]"
value="$view[usrhomepage]"
size="20" maxlength="256">
</font></td>
</tr>

<!-- Password -->
<tr>
<td width="30%" bgcolor="#A5A595" align="center">
<font size="2" color="white"><b>비밀번호</b></font></td>
<td width="70%" bgcolor="white" align="left">
<font color="white">
<input type="password" name="fil[pw]"
size="10" maxlength="20">
</font></td>
</tr>

<!-- Subject -->
<tr>
<td width="30%" bgcolor="#A5A595" align="center">
<font size="2" color="white"><b>글 제목</b></font></td>
<td width="70%" bgcolor="white" align="left">
<font color="white">
<input type="text" name="fil[title]"
size="30" maxlength="256" value="$view[usrtitle]">
</font></td>
</tr>

<!-- Text -->
<tr>
<td width="30%" bgcolor="#C5C5B5" align="center">
<font size="2" color="white"><b>본 문</b></font></td>
<td width="70%" bgcolor="white" align="left">
<font color="white">
<textarea name="fil[text]" wrap="hard" rows="10" cols="36">
$view[text]</textarea>
</font></td>
</tr>
</table>
비밀 번호 : <input type="password" name="modpw">
<input type="submit" value="수정">
<input type="reset" value="재작성">
</form>

<table width="550" cellspacing="1"
border="0" cellpadding="2">
<tr>
<td align="right"><font size="2"><b>
|<a href="list.php3?board=$board&page=$nowpage">
게시물 목록</a>
|<a href="view.php3?board=$board&no=$no">
읽기</a> |
|<a href="delete.php3?board=$board&no=$no">
삭제</a> |
</b></font></td>
</tr>
</table>

</center>
</body>

</html>
"); // 전체 화면 출력 완료
?>


f. 쿠키 넣기

글을 한 번이라도 저장하면 그 이후에는 글 작성시 작성자명 같은 거는 자동으로 입력되어 있으면 편리할 것입니다. 이걸 하기 위해 우리는 쿠키를 사용할 것이고 이를 위해서 우리는 글 입력에서의 input tag 에서 뜬금없는(?) value 값을 넣었죠.

value="$mwbwd[homepage]"

이런거요. :] 이걸 제대로 사용하려면 글 입력때 쿠키를 발생시켜줘야 합니다.

쿠키를 만드는 함수는 setcookie 입니다.
함수 사용법은 이미 언급했었구요. :]

이미 배웠으니 곧바로 적용해볼까요?

$expi = 60 * 60 * 24 * 30;
setcookie("mwbwd[name]", $fil[name], time() + $expi);
setcookie("mwbwd[email]", $fil[email], time() + $expi);
setcookie("mwbwd[homepage]", $fil[homepage], time() + $expi);

이겁니다. :] 여기서 잠깐!

$expi = 60 * 60 * 24 * 30;

는 무슨 계산일까요? 뭐긴요. 날짜 계산이죠.
쿠키는 초 단위로 유효 기간이 지정됩니다.

에. 그리니까 쿠키는 기간을 정해주면 지정해준 기간만큼 여러분의 하드 속에 살아있습니다.
그리고 기간이 되면 사라지죠.
기간을 지정안할 경우 웹 브라우저가 종료되면 삭제되구요.

그런데 우리는 글 입력시 자동으로 작성자명과 email, homepage 등이 자동으로 입력되게 하는 것을 몇 초동안만 유지하는게 아니지요.
적어도 몇 일 단위이어야 합니다.

자. 1시간은 몇 초일까요? 1분은 60초죠?
1시간은 60분이구요. 즉 1시간은 60 * 60, 3600초입니다. 그리고 하루는 24시간이죠.
즉 하루는 3600 * 24 초입니다. 그리하야

$expi = 60 * 60 * 24 ...

식이 나오죠. 그리고 그 뒤의 30은 해당 쿠키를 30일간 유지하겠다는 의미입니다.
7 이라고 하면 7일(1주일)간 유지되겠죠.

이걸 run.php3 에서 글을 입력한 뒤에 실행시켜야죠.

$result = mysql_query("INSERT INTO $board VALUES('',
$ist[num], $ist[idx], '$fil[title]', '$fil[text]',
0, 0, 0, 0, 0, '', '', 0, '$fil[name]', '$passwd',
'$fil[email]', '$fil[homepage]', $intime)", $connect);

$expi = 60 * 60 * 24 * 30;
setcookie("mwbwd[name]", $fil[name], time() + $expi);
setcookie("mwbwd[email]", $fil[email], time() + $expi);
setcookie("mwbwd[homepage]", $fil[homepage], time() + $expi);

웃. $result ... 부분이 바로 글 입력하는 겁니다. 이거는 입력하지 마시구요.
$expi 부분부터 추가하시면 됩니다. 글 입력 부분에요.
만약을 위해서 글 입력 부분 소스를 쓰죠 --;

if ($mode == "write") {
check_input($fil[name], $fil[email], $fil[text]);
$result = mysql_query("
SELECT MAX(num) AS num, MAX(idx) AS idx,
MAX(no) AS no FROM $board", $connect);

$ist[num] = mysql_result($result, 0, "num");
$ist[num] += 1;
$ist[idx] = mysql_result($result, 0, "idx");
$ist[idx] += 1;

mysql_free_result($result);

$intime = time();
$passwd = crypt($fil[pw]);

$result = mysql_query("INSERT INTO $board VALUES('',
$ist[num], $ist[idx], '$fil[title]', '$fil[text]',
0, 0, 0, 0, 0, '', '', 0, '$fil[name]', '$passwd',
'$fil[email]', '$fil[homepage]', $intime)", $connect);

$expi = 60 * 60 * 24 * 30;
setcookie("mwbwd[name]", $fil[name], time() + $expi);
setcookie("mwbwd[email]", $fil[email], time() + $expi);
setcookie("mwbwd[homepage]", $fil[homepage], time() + $expi);
}

이렇게요.


g. 완료 후 페이지 이동

글을 입력했거나 글을 수정하는 등의 작업이 완료되면 페이지 이동을 시켜야 합니다.
무슨 말인고하면 글 쓸 뒤에는 자동으로 글 목록 화면 1 페이지로 이동하고, 글 수정 후에는 수정한 글 내용 보기로 이동하거나요.

이러한 작업을 하기 위해서는 Header 함수를 쓰면 됩니다. Header 함수는 서버가 클라이언트에게
정보를 보낼 때 처음에 달라붙는 정보를 지정하는 겁니다. 보통 자료실 운영에서 자료를 다운로드하게 하거나 페이지 이동할 때 쓰게 되더군요. --;

Header("Location: http://www.creple.com");

이라고 하면 http://www.creple.com 으로 이동합니다. 이외 다른 용도는 자료실 기능을
붙일 때 설명하구요. 일단은 주소 이동때 쓰도록 하죠. ^^;

물론 아래의 각 소스는 맨 마지막에(해당 모드의) 들어가야 합니다.

- 글 쓰고 난 후
Header("Location: list.php3?board=$board");

 

- 글 수정 후
Header("Location: view.php3?board=$board&no=$oldarti[no]");

 

- 글 삭제 후
Header("Location: list.php3?board=$board");

그다지 어려운 게 없습니다. :] 자. 여기서 답 안드리는 문제를 내겠습니다.
글 삭제 후 삭제한 글이 위치했던 페이지로 이동시켜보세요.

만약 총 10페이지에서 5페이지에 위치한 글을 삭제했습니다.
그런데 위의 방식이라면 5페이지의 글을 삭제했어도 1페이지로 이동하죠.
이걸 1페이지대신 5페이지로 이동시키는 겁니다.

이걸 구현하기 위한 함수등은 모두 준비되어 있답니다. ^^

에헴. 이번 강좌는 설명보다 소스 출력이 더 많았군요.
^^; 잔머리.....가 맞긴 한데 이미 이전 강좌들에서 설명할 것들을 거의 다 해놔서 따로 설명 적지 않았습니다.^^;

이 강좌를 쓰는 저는 이 강좌를 쓰며 함께 게시판을 만들고 있답니다. 그래서 강좌에서 70만큼 게시판 제작을
진행하면 강좌의 게시판을 저도 70정도 만든거죠.

이 강좌의 게시판이 만들어지는 과정은
http://coco.st/~dimanche/exbd/list.php3?board=testboard
에서 볼 수 있습니다. ^^;

다음 강좌에서 뵈요.

----------------------------
함께하면 즐거운 사이트들 (-_-; 광고임 -_-;)
http://game.creple.com/delthia
http://creple.com
http://coco.st
----------------------------


- ?! 디망쉬

 

관련자료

댓글 0
등록된 댓글이 없습니다.

공지사항


뉴스광장


  • 현재 회원수 :  60,041 명
  • 현재 강좌수 :  35,855 개
  • 현재 접속자 :  106 명