16 장 레슨문의 게시판

30
16 장 장장장장 장장장

Upload: marlo

Post on 20-Mar-2016

122 views

Category:

Documents


10 download

DESCRIPTION

16 장 레슨문의 게시판. 답변글을 위한 DB 테이블 설계 방법 답변글 처리하는 알고리즘을 이해 답변글의 들여쓰기 메인글 / 답변글 / 수정글 저장 기법. 레슨 문의 게시판 개요 DB 테이블 설계 및 생성 글 목록 확인 글 내용 확인 글 작성. 1.1 결과 화면과 요구사항. 요구사항 HTML 쓰기가 가능한 게시판 (12 장 가입인사 게시판의 기능 ) 답변글 작성 가능. [ 그림 16-1] 레슨 문의 게시판의 글 목록 페이지. 1.2 준비 파일과 페이지 구성. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 16 장 레슨문의 게시판

16 장 레슨문의 게시판

Page 2: 16 장 레슨문의 게시판

IT CookBook,PHP 웹 프로그래밍 입문 ( 개정판 )

[ 강의교안 이용 안내 ]• 본 강의교안의 저작권은 한빛아카데미㈜에 있습니다 . • 이 자료를 무단으로 전제하거나 배포할 경우 저작권법 136 조에 의거하여 최고 5 년

이하의 징역 또는 5 천만원 이하의 벌금에 처할 수 있고 이를 병과 ( 倂科 ) 할 수도 있습니다 .

Page 3: 16 장 레슨문의 게시판

학습목표 답변글을 위한 DB 테이블 설계 방법 답변글 처리하는 알고리즘을 이해 답변글의 들여쓰기 메인글 / 답변글 / 수정글 저장 기법

Page 4: 16 장 레슨문의 게시판

목차1. 레슨 문의 게시판 개요2. DB 테이블 설계 및 생성3. 글 목록 확인4. 글 내용 확인5. 글 작성

Page 5: 16 장 레슨문의 게시판

1.1 결과 화면과 요구사항

• 요구사항 HTML 쓰기가 가능한 게시판 (12 장 가입인사 게시판의 기능 ) 답변글 작성 가능

[ 그림 16-1] 레슨 문의 게시판의 글 목록 페이지

Page 6: 16 장 레슨문의 게시판

1.2 준비 파일과 페이지 구성

구분 파일 / 폴더명 기능

데이터베이스 qna.sql 메인 글의 데이터베이스 테이블 생성

페이지

list.php 글 목록 페이지

view.php 글 내용 보기 페이지

write_form.php 글쓰기 페이지 ( 수정 , 답변 포함 )

기능insert.php 글 저장 ( 수정 , 답변 포함 )

delete.php 글 삭제 ( 수정 , 답변 포함 )

[ 표 16 -1] 레슨 문의 게시판 제작 실습에서 사용하는 파일 목록

Page 7: 16 장 레슨문의 게시판

1.2 준비 파일과 페이지 구성

[ 그림 16-2] 레슨 문의 게시판의 페이지와 기능 흐름

Page 8: 16 장 레슨문의 게시판

1.3 답변글 처리 방법

[ 그림 16-4] 새로운 답변글의 설정값과 ord 값의 변화

Page 9: 16 장 레슨문의 게시판

2. DB 테이블 설계 및 생성

필드명 형식 추가 사항 설명

num intnot null, auto_increment, pri-mary key

일련번호

group_num int not null 그룹 번호

depth int not null 답변글 깊이

ord int not null 답변글 순서

id char(15) not null 아이디

name char(10) not null 이름

nick char(10) not null 닉네임

subject char(100)

not null 제목

content text not null 글 내용

regist_day char(20) 작성일

hit int 조회수

is_html char(1) HTML 쓰기

[ 표 16-2] 레슨 문의 데이터베이스 테이블 ( 테이블명 :qna)

Page 10: 16 장 레슨문의 게시판

qna 데이터베이스 테이블 생성 qna.sql create table qna ( num int not null auto_increment, group_num int not null, depth int not null, ord int not null, id char(15) not null, name char(10) not null, nick char(10) not null, subject char(100) not null, content text not null, regist_day char(20), hit int, is_html char(1), primary key(num) );

Page 11: 16 장 레슨문의 게시판

3. 글 목록 확인

[ 그림 16-5] 글 목록 페이지

[ 참고 ] 게시물의 제목 맨 뒤에 표시된 메인글과 답변글 표시는 자동으로 입력된 것이 아닌 , 답변글 관계를 파악하기 쉽도록 제목에 입력한 것이다 .

Page 12: 16 장 레슨문의 게시판

예제 16-1 레슨 문의 게시판의 글 목록 페이지 list,php001 <?002 session_start(); 003 $table="qna";004 ?>005 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">006 <html>007 <head>008 <meta charset="euc-kr">009 <link href="../css/common.css" rel="stylesheet" type="text/css" me-dia="all">010 <link href="../css/board1.css" rel="stylesheet" type="text/css" me-dia="all">011 </head>012 <?013 include "../lib/dbconn.php";014 $scale=10; // 한 화면에 표시되는 글의 개수

<!-- 생략 -->034 $sql="select * from $table order by group_num desc, ord asc";035 }036

Page 13: 16 장 레슨문의 게시판

예제 16-1 레슨 문의 게시판의 글 목록 페이지 list,php037 $result=mysql_query($sql, $connect);038 $total_record=mysql_num_rows($result); // 전체 글의 개수039040 // 전체 페이지 수 ($total_page) 계산041 if($total_record % $scale==0)042 $total_page= floor($total_record/$scale);043 else044 $total_page= floor($total_record/$scale) + 1;045046 if(!$page) // 페이지번호 ($page)가 0 일 때047 $page=1; // 페이지 번호를 1 로 초기화048049 // 표시할 페이지 ($page) 에 따라 $start 계산050 $start=($page - 1) * $scale;051 $number= $total_record - $start;052 ?>053 <body>

<!-- 생략 -->

Page 14: 16 장 레슨문의 게시판

예제 16-1 레슨 문의 게시판의 글 목록 페이지 list,php100 <div id="list_content">101 <?102 for($i=$start; $i<$start+$scale && $i<$total_record; $i++) 103 {104 mysql_data_seek($result, $i); 105 $row=mysql_fetch_array($result); 106 107 $item_num=$row[num];108 $item_id=$row[id];109 $item_name=$row[name];110 $item_nick=$row[nick];111 $item_hit=$row[hit];112 $item_date=$row[regist_day];113 $item_date=substr($item_date, 0, 10);114 $item_subject=str_replace(" ", "&nbsp;", $row[subject]);115 $item_depth=$row[depth];116117 $space="";118 for($j=0; $j<$item_depth; $j++)119 $space="&nbsp;&nbsp;".$space;

Page 15: 16 장 레슨문의 게시판

예제 16-1 레슨 문의 게시판의 글 목록 페이지 list,php120121 ?>122 <div id="list_item">123 <div id="list_item1"><?= $number ?></div>124 <div id="list_item2"><?=$space?> <a href="view.php?table=<?=$table?>&num=<?=$item_num?>&page=<?=$-page?>"> <?= $item_subject ?></a></div>125 <div id="list_item3"><?= $item_nick ?></div>126 <div id="list_item4"><?= $item_date ?></div>127 <div id="list_item5"><?= $item_hit ?></div>128 </div>129 <?130 $number--;131 } // end of for 문 (102 행 )132 ?>

<!-- 생략 -->170 </body>171 </html>

Page 16: 16 장 레슨문의 게시판

4. 글 내용 확인

[ 그림 16-6] 글 내용 보기 페이지 _ 답변글

Page 17: 16 장 레슨문의 게시판

예제 16-2 레슨 문의 게시판의 글 내용 보기 페이지 view.php<!-- 생략 -->

077 <div id="view_button">078 <a href="list.php?table=<?=$table?>&page=<?=$page?>"> <img src="../img/list.png"></a>&nbsp;079 <? 080 if($userid && ($userid==$item_id) )081 {082 ?>083 <a href="write_form.php?table=<?=$table?>&mode=modify&num=<?=$num?>& page=<?=$page?>"><img src="../img/modify.png"></a>&nbsp;084 <a href="javascript:del('delete.php?table=<?=$table?>&num=<?=$num?>')"> <img src="../img/delete.png"></a>&nbsp;085 <?086 }087 ?>

Page 18: 16 장 레슨문의 게시판

예제 16-2 레슨 문의 게시판의 글 내용 보기 페이지 view.php088 <? 089 if($userid)090 {091 ?>092 <a href="write_form.php?table=<?=$table?>&mode=response&num=<?=$num?> &page=<?=$page?>"><img src="../img/response.png"></a>&nbsp;093 <a href="write_form.php?table=<?=$table?>"><img src="../img/write.png"></a>094 <?095 }096 ?>097 </div>

<!-- 생략 -->

Page 19: 16 장 레슨문의 게시판

5.1 글쓰기 페이지 생성

[ 그림 16-7] 글쓰기 페이지 ( 순서대로 새로운 글쓰기 , 글 수정 , 답변글 쓰기 )

Page 20: 16 장 레슨문의 게시판

예제 16-3 레슨 문의 게시판의 글쓰기 페이지 write_form.php001 <?002 session_start();003 ?>004 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN“ "http://www.w3.org/TR/html4/loose.dtd">005 <html>006 <head>007 <link href="../css/common.css" rel="stylesheet" type="text/css" me-dia="all">008 <link href="../css/board1.css" rel="stylesheet" type="text/css" me-dia="all">009 <meta charset="euc-kr">010 </head>011 <?012 if($mode=="modify" || $mode=="response")013 {014 include "../lib/dbconn.php";015016 $sql="select * from $table where num=$num";017 $result=mysql_query($sql, $connect);018 $row=mysql_fetch_array($result);019

Page 21: 16 장 레슨문의 게시판

예제 16-3 레슨 문의 게시판의 글쓰기 페이지 write_form.php020 $item_subject=$row[subject];021 $item_content=$row[content];022023 if($mode=="response")024 {025 $item_subject="[re]".$item_subject;026 $item_content=">".$item_content;027 $item_content=str_replace("\n", "\n>", $item_content);028 $item_content="\n\n".$item_content;029 }030 mysql_close();031 }032 ?>033 <body>

<!-- 생략 -->059 <?060 if($mode=="modify")061 {062 ?>

Page 22: 16 장 레슨문의 게시판

예제 16-3 레슨 문의 게시판의 글쓰기 페이지 write_form.php063 <form name="board_form" method="post" action="insert.php? mode=modify& num=<?=$num?>&page=<?=$page?>&table=<?=$table?>"> 064 <?065 }066 elseif($mode=="response")067 {068 ?>069 <form name="board_form" method="post" action="insert.php? mode=response&num=<?=$num?>&page=<?=$page?>&table=<?=$table?>"> 070 <?071 }072 else073 {074 ?>075 <form name="board_form" method="post" action="insert.php?table=<?=$table?>"> 076 <?077 }078 ?>

Page 23: 16 장 레슨문의 게시판

예제 16-3 레슨 문의 게시판의 글쓰기 페이지 write_form.php079 <div id="write_form">080 <div class="write_line"></div>081 <div id="write_row1">082 <div class="col1"> 닉네임 </div>083 <div class="col2"><?=$usernick?></div>084 <?085 if($userid && ($mode!="modify") && ($mode!="response") )086 {087 ?>088 <div class="col3"><input type="checkbox" name="html_ok" value="y"> HTML 쓰기 </div>089 <?090 }091 ?>092 </div>093 <div class="write_line"></div>094 <div id="write_row2"><div class="col1"> 제목 </div>095 <div class="col2"> <input type="text" name="subject" value="<?=$item_subject?>" ></div>

Page 24: 16 장 레슨문의 게시판

예제 16-3 레슨 문의 게시판의 글쓰기 페이지 write_form.php096 </div>097 <div class="write_line"></div>098 <div id="write_row3"><div class="col1"> 내용 </div>099 <div class="col2"><textarea rows="15" cols="79" name="content"> <?=$item_content?></textarea></div>100 </div>101 <div class="write_line"></div>102 </div>103104 <div id="write_button"><input type="image" src="../img/ok.png">&nbsp;105 <a href="list.php?table=<?=$table?>&page=<?=$page?>"> <img src="../img/list.png"></a>106 </div>107 </form>

<!-- 생략 -->113 </body>114 </html>

Page 25: 16 장 레슨문의 게시판

예제 16-4 레슨 문의 글 저장 insert.php001 <?002 session_start();003 ?>004005 <meta charset="euc- kr">006 <?

<!-- 생략 -->040 $regist_day=date("Y-m-d (H:i)"); // 현재의 ' 년 - 월 - 일 -시 - 분 ' 을 저장041 include "../lib/dbconn.php"; // dconn.php 파일을 불러옴042043 if($mode=="modify")044 {045 $sql="update $table set subject='$subject', content='$content‘ where num=$num";046 mysql_query($sql, $connect); // $sql 에 저장된 명령 실행047 }048 else049 {

Page 26: 16 장 레슨문의 게시판

예제 16-4 레슨 문의 글 저장 insert.php050 if($html_ok=="y")051 {052 $is_html="y";053 }054 else055 {056 $is_html="";057 $content=htmlspecialchars($content);058 }059 060 if($mode=="response")061 {062 // 부모글 가져오기063 $sql="select * from $table where num = $num";064 $result=mysql_query($sql, $connect);065 $row=mysql_fetch_array($result);068 $group_num=$row[group_num];069 $depth=$row[depth] + 1;070 $ord=$row[ord] + 1;

Page 27: 16 장 레슨문의 게시판

예제 16-4 레슨 문의 글 저장 insert.php071072 // 해당 그룹에서 ord 가 부모글의 ord($row[ord]) 보다 큰 경우엔073 // ord 값 1 증가시킴074 $sql="update $table set ord = ord + 1 where group_num = $row[group_num] and ord > $row[ord]";075 mysql_query($sql, $connect); 076077 // 레코드 삽입078 $sql="insert into $table (group_num, depth, ord, id, name, nick, subject,";079 $sql.="content, regist_day, hit, is_html) ";080 $sql.="values($group_num, $depth, $ord, '$userid','$username', '$usernick', '$subject',";081 $sql.="'$content', '$regist_day', 0, '$is_html')"; 082083 mysql_query($sql, $connect); // $sql 에 저장된 명령 실행084 }085 else086 {

Page 28: 16 장 레슨문의 게시판

예제 16-4 레슨 문의 글 저장 insert.php087 $depth=0; // depth, ord 를 0 으로 초기화 088 $ord=0;089 090 // 레코드 삽입 (group_num 제외 )091 $sql="insert into $table(depth, ord, id, name, nick, subject,";092 $sql.="content, regist_day, hit, is_html)";093 $sql.="values($depth, $ord, '$userid', '$username', '$user-nick' ";094 $sql.="'$subject', '$content', '$regist_day', 0, '$is_html')"; 095 mysql_query($sql, $connect); // $sql 에 저장된 명령 실행096097098 $sql="select last_insert_id()"; 099 $result=mysql_query($sql, $connect);100 $row=mysql_fetch_array($result);101 $auto_num=$row[0];

Page 29: 16 장 레슨문의 게시판

예제 16-4 레슨 문의 글 저장 insert.php102 103104 $sql="update $table set group_num = $auto_num where num=$auto_num";105 mysql_query($sql, $connect);106 }107 }108 109 mysql_close(); // 데이터베이스와 연결 종료110111 echo ("112 <script>113 location.href='list.php?table= $table&page= $page';114 </script>115 ");116 ?>

Page 30: 16 장 레슨문의 게시판

last_insert_id() 함수• 형식 select last_insert_id()

• 기능 MySQL 의 insert into 명령을 실행할 때 , 레코드의 기본키 같이 자동으로 증가한 값을 가져온다 .

• 사용 예 01 insert into test (auto, text) values (NULL, 'text'); 02 select last_insert_id()

auto 가 자동 증가 (auto_increment) 필드이며 , 그 전의 값이 4 라고 가정하자 . insert into 명령이 실행되면서 auto 에는 자동으로 5 가 입력되는데 , last_insert_id() 의 반환값에도 5 가 저장된다 .