1 / 30

16 장 레슨문의 게시판

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

marlo
Download Presentation

16 장 레슨문의 게시판

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 16장 레슨문의 게시판

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

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

  4. 1.1 결과 화면과 요구사항 • 요구사항 • HTML 쓰기가 가능한 게시판(12장 가입인사 게시판의 기능) • 답변글 작성 가능 [그림 16-1] 레슨 문의 게시판의 글 목록 페이지

  5. 1.2 준비 파일과 페이지 구성 [표 16 -1] 레슨 문의 게시판 제작 실습에서 사용하는 파일 목록

  6. 1.2 준비 파일과 페이지 구성 [그림 16-2] 레슨 문의 게시판의 페이지와 기능 흐름

  7. 1.3 답변글 처리 방법 [그림 16-4] 새로운 답변글의 설정값과 ord값의 변화

  8. 2. DB 테이블 설계 및 생성 [표 16-2] 레슨 문의 데이터베이스 테이블(테이블명:qna)

  9. 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) );

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

  11. 예제 16-1 레슨 문의 게시판의 글 목록 페이지 list,php 001 <? 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" media="all"> 010 <link href="../css/board1.css" rel="stylesheet" type="text/css" media="all"> 011 </head> 012 <? 013 include "../lib/dbconn.php"; 014 $scale=10; //한 화면에 표시되는 글의 개수 <!-- 생략 --> 034 $sql="select * from $table order by group_numdesc, ordasc"; 035 } 036

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

  13. 예제 16-1 레슨 문의 게시판의 글 목록 페이지 list,php 100 <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]; 116 117 $space=""; 118 for($j=0; $j<$item_depth; $j++) 119 $space="&nbsp;&nbsp;".$space;

  14. 예제 16-1 레슨 문의 게시판의 글 목록 페이지 list,php 120 121 ?> 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>

  15. 4. 글 내용 확인 [그림 16-6] 글 내용 보기 페이지_답변글

  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 ?>

  17. 예제 16-2 레슨 문의 게시판의 글 내용 보기 페이지 view.php 088 <? 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> <!-- 생략 -->

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

  19. 예제 16-3 레슨 문의 게시판의 글쓰기 페이지 write_form.php 001 <? 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" media="all"> 008 <link href="../css/board1.css" rel="stylesheet" type="text/css" media="all"> 009 <meta charset="euc-kr"> 010 </head> 011 <? 012 if($mode=="modify" || $mode=="response") 013 { 014 include "../lib/dbconn.php"; 015 016 $sql="select * from $table where num=$num"; 017 $result=mysql_query($sql, $connect); 018 $row=mysql_fetch_array($result); 019

  20. 예제 16-3 레슨 문의 게시판의 글쓰기 페이지 write_form.php 020 $item_subject=$row[subject]; 021 $item_content=$row[content]; 022 023 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 ?>

  21. 예제 16-3 레슨 문의 게시판의 글쓰기 페이지 write_form.php 063 <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 else 073 { 074 ?> 075 <form name="board_form" method="post" action="insert.php?table=<?=$table?>"> 076 <? 077 } 078 ?>

  22. 예제 16-3 레슨 문의 게시판의 글쓰기 페이지 write_form.php 079 <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>

  23. 예제 16-3 레슨 문의 게시판의 글쓰기 페이지 write_form.php 096 </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> 103 104 <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>

  24. 예제 16-4 레슨 문의 글 저장 insert.php 001 <? 002 session_start(); 003 ?> 004 005 <meta charset="euc- kr"> 006 <? <!-- 생략 --> 040 $regist_day=date("Y-m-d (H:i)"); // 현재의 '년-월-일-시-분'을 저장 041 include "../lib/dbconn.php"; // dconn.php 파일을 불러옴 042 043 if($mode=="modify") 044 { 045 $sql="update $table set subject='$subject', content='$content‘ where num=$num"; 046 mysql_query($sql, $connect); // $sql 에 저장된 명령 실행 047 } 048 else 049 {

  25. 예제 16-4 레슨 문의 글 저장 insert.php 050 if($html_ok=="y") 051 { 052 $is_html="y"; 053 } 054 else 055 { 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;

  26. 예제 16-4 레슨 문의 글 저장 insert.php 071 072 // 해당 그룹에서 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); 076 077 // 레코드 삽입 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')"; 082 083 mysql_query($sql, $connect); // $sql 에 저장된 명령 실행 084 } 085 else 086 {

  27. 예제 16-4 레슨 문의 글 저장 insert.php 087 $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', '$usernick' "; 094 $sql.="'$subject', '$content', '$regist_day', 0, '$is_html')"; 095 mysql_query($sql, $connect); // $sql 에 저장된 명령 실행 096 097 098 $sql="select last_insert_id()"; 099 $result=mysql_query($sql, $connect); 100 $row=mysql_fetch_array($result); 101 $auto_num=$row[0];

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

  29. 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가 저장된다.

More Related