Web Programming >> PHP Programming
|
[목차] |
제4장 파일처리 프로그램
2. 회원관리 예제 1 : 회원관리 - 목록(list.html) <HTML> <HEAD> <TITLE> Leelab - 파일 처리(3)</TITLE> </HEAD> <BODY BGCOLOR="#FFFFFF"> <? $memberfile = "./member_file.data"; $fp = fopen($memberfile, "r"); if(!$fp) { echo("File Not Found !"); exit; } ?> <TABLE width=600 align=center> <TR><td align=center> <font size=3><b>[ 회원 관리 ]</b></font> </td></TR> </TABLE> <br> <TABLE width=600 align=center border=1> <TR> <TD width=50 align=center><font size=2>번호</TD> <TD width=80 align=center><font size=2>이름</TD> <TD width=70 align=center><font size=2>id</TD> <TD width=100 align=center><font size=2>전화번호</TD> <TD width=100 align=center><font size=2>휴대전화</TD> <TD width=100 align=center><font size=2>가입일</TD> </TR> <? while(!feof($fp)) { $char_data = fgetc($fp); // 한자씩 읽어와서 if($char_data != "\n") // 한 목록씩 파싱 $line_data .= $char_data; else { $col_data = explode(" || ", $line_data); // 각 항목별로 파싱 $number = $col_data[0]; $name = $col_data[1]; $id = $col_data[2]; $address = $col_data[3]; $phone = $col_data[4]; $handphone = $col_data[5]; $email = $col_data[6]; $homepage = $col_data[7]; $date = $col_data[8]; $count = $col_data[9]; ?> <TR> <TD><font size=2> <?echo("$number");?></TD> <TD><font size=2> <a href="view.html?number=<?echo("$number");?>"><?echo("$name");?></a></TD> <TD><font size=2> <?echo("$id");?></TD> <TD><font size=2> <?echo("$phone");?></TD> <TD><font size=2> <?echo("$handphone");?></TD> <TD><font size=2> <?echo("$date");?></TD> </TR> <? $line_data = ""; } } fclose($fp); ?> </TABLE> <br> <TABLE width=600 align=center> <TR> <td align=left><font size=2><a href="list.html">목록</a></td> <td align=right><font size=2><a href="write.html">등록</a></td> </TR> </TABLE> </BODY> </HTML>예제 2 : 회원관리 - 쓰기(write.html) <HTML> <HEAD> <TITLE> Leelab - 파일 처리(2)</TITLE> </HEAD> <BODY BGCOLOR="#FFFFFF"> <? if(!strcmp($mode, "process")) { $memberfile = "./member_file.data"; if($fp = fopen($memberfile, 'r')) { $line_data = fread($fp, 80); // 맨 첫 라인을 읽어옴 $member = explode(" || ", $line_data); // 번호를 파싱 $number = $member[0]+1; // 다음 번호를 얻어옴 fclose($fp); } $date = time(); $count = 1; $member_data[0] = $number; $member_data[1] = $name; $member_data[2] = $id; $member_data[3] = $address; $member_data[4] = $phone; $member_data[5] = $handphone; $member_data[6] = $email; $member_data[7] = $homepage; $member_data[8] = $date; $member_data[9] = $count; $member_record = implode($member_data, " || "); $file_size = filesize($memberfile); if($fp = fopen($memberfile, 'r')) { // 내용 읽어오기 $member_file_data = fread($fp, $file_size); fclose($fp); } if($fp = fopen($memberfile, 'w')) { fwrite($fp, $member_record, strlen($member_record)); fwrite($fp, "\n", strlen("\n")); fwrite($fp, $member_file_data, strlen($member_file_data)); fclose($fp); echo("<br>등록하였습니다.<br> 잠시 기다리시면 목록화면으로 갑니다.<BR>\n"); echo("이름 : $name <BR>\n"); echo("주소 : $address <BR>\n"); echo("전화 : $phone <BR>\n"); echo("휴대전화 : $handphone <BR>\n"); echo("아이디 : $id <BR>\n"); echo("전자메일 : $email <BR>\n"); echo("홈페이지 : $homepage <BR>\n"); echo("가입일 : $date <BR>\n"); echo("카운터 : $count <BR>\n"); echo ("<meta http-equiv='Refresh' content='3; URL=list.html'>"); } else { echo("${memberfile}을 여는데 실패했습니다."); } }else { ?> <TABLE width=600 align=center> <TR><td align=center> <font size=3><b>[ 회원 관리 ]</b></font> </td></TR> </TABLE> <br> <TABLE width=600 align=center border=1> <FORM METHOD=POST ACTION=""><br> <TR> <TD width=150 align=center><font size=2>이름</TD> <TD align=center><INPUT TYPE="text" NAME="name"></TD> </tr> <TR> <TD width=150 align=center><font size=2>ID</TD> <TD align=center><INPUT TYPE="text" NAME="id"></TD> </tr> <TR> <TD width=150 align=center><font size=2>주소</TD> <TD align=center><INPUT TYPE="text" NAME="address"></TD> </tr> <TR> <TD width=150 align=center><font size=2>전화</TD> <TD align=center><INPUT TYPE="text" NAME="phone"></TD> </tr> <TR> <TD width=150 align=center><font size=2>휴대폰</TD> <TD align=center><INPUT TYPE="text" NAME="handphone"></TD> </tr> <TR> <TD width=150 align=center><font size=2>E-Mail</TD> <TD align=center><INPUT TYPE="text" NAME="email"></TD> </tr> <TR> <TD width=150 align=center><font size=2>Homepage</TD> <TD align=center><INPUT TYPE="text" NAME="homepage"></TD> </tr> <TR> <TD align=center colspan=2><INPUT TYPE="submit" value="등록신청"></TD> </tr> <INPUT TYPE="hidden" Name="mode" value="process"> </FORM> </table> <? } ?> </BODY> </HTML> 예제 3 : 회원관리 - 회원 세부내역 보기(view.html) <HTML> <HEAD> <TITLE> Leelab - 파일 처리(3)</TITLE> </HEAD> <BODY BGCOLOR="#FFFFFF"> <? if($number == "") { echo ("<meta http-equiv='Refresh' content='3; URL=list.html'>"); exit; } $memberfile = "./member_file.data"; $file_size = filesize($memberfile); if($fp = fopen($memberfile, 'r')) { // 내용 읽어오기 $member_file_data = fread($fp, $file_size); fclose($fp); } else { echo("${memberfile}을 여는데 실패했습니다."); exit; } $member_line = explode("\n", $member_file_data); // 각 레코드별로 파싱 $no = 0; while($member_data = $member_line[$no++]) { // 각 레코드를 검사 $col_data = explode(" || ", $member_data); if($col_data[0] == $number) { // 내용을 보여줄 레코드이면 반복문을 빠져나감 $name = $col_data[1]; $id = $col_data[2]; $address = $col_data[3]; $phone = $col_data[4]; $handphone = $col_data[5]; $email = $col_data[6]; $homepage = $col_data[7]; $date = $col_data[8]; $count = $col_data[9]; break; } } echo("<b>[ 회원 세부사항 보기 ]</b> <BR><br>\n"); echo("번호 : $number <BR>\n"); echo("이름 : $name <BR>\n"); echo("아이디 : $id <BR>\n"); echo("주소 : $address <BR>\n"); echo("전화 : $phone <BR>\n"); echo("휴대전화 : $handphone <BR>\n"); echo("전자메일 : $email <BR>\n"); echo("홈페이지 : $homepage <BR>\n"); echo("가입일 : $date <BR>\n"); echo("카운터 : $count <BR>\n"); ?> <br> <font size=2><a href="list.html">목록</a> <font size=2><a href="modify.html?number=<?echo("$number");?>">수정</a> <font size=2><a href="delete.html?number=<?echo("$number");?>">삭제</a> </BODY> </HTML>예제 4 : 회원관리 - 수정하기(modify.html) <HTML> <HEAD> <TITLE> Leelab - 파일 처리(2)</TITLE> </HEAD> <BODY BGCOLOR="#FFFFFF"> <? if($number == "") { echo ("<meta http-equiv='Refresh' content='3; URL=list.html'>"); exit; } $memberfile = "./member_file.data"; if(!strcmp($mode, "process")) { $col_data[0] = $number; $col_data[1] = $name; $col_data[2] = $id; $col_data[3] = $address; $col_data[4] = $phone; $col_data[5] = $handphone; $col_data[6] = $email; $col_data[7] = $homepage; $file_size = filesize($memberfile); if($fp = fopen($memberfile, 'r')) { // 내용 읽어오기 $member_file_data = fread($fp, $file_size); fclose($fp); } else { echo("${memberfile}을 여는데 실패했습니다."); exit; } $member_line = explode("\n", $member_file_data); // 각 레코드별로 파싱 if($fp = fopen($memberfile, 'w')) { $no = 0; while($member_data = $member_line[$no++]) { // 각 레코드를 검사 $col_member_data = explode(" || ", $member_data); if($col_member_data[0] == $number) { // 내용을 보여줄 레코드이면 반복문을 빠져나감 $col_data[8] = $date = $col_member_data[8]; $col_data[9] = $count = $col_member_data[9]; $member_record = implode($col_data, " || "); fwrite($fp, $member_record, strlen($member_record)); fwrite($fp, "\n", strlen("\n")); }else { fwrite($fp, $member_data, strlen($member_data)); fwrite($fp, "\n", strlen("\n")); } } } else { echo("${memberfile}을 여는데 실패했습니다."); exit; } echo("<br>수정하였습니다.<br> 잠시 기다리시면 목록화면으로 갑니다.<BR>\n"); echo("이름 : $name <BR>\n"); echo("주소 : $address <BR>\n"); echo("전화 : $phone <BR>\n"); echo("휴대전화 : $handphone <BR>\n"); echo("아이디 : $id <BR>\n"); echo("전자메일 : $email <BR>\n"); echo("홈페이지 : $homepage <BR>\n"); echo("가입일 : $date <BR>\n"); echo("카운터 : $count <BR>\n"); echo ("<meta http-equiv='Refresh' content='3; URL=list.html'>"); }else { $file_size = filesize($memberfile); if($fp = fopen($memberfile, 'r')) { // 내용 읽어오기 $member_file_data = fread($fp, $file_size); fclose($fp); } else { echo("${memberfile}을 여는데 실패했습니다."); exit; } $member_line = explode("\n", $member_file_data); // 각 레코드별로 파싱 $no = 0; while($member_data = $member_line[$no++]) { // 각 레코드를 검사 $col_data = explode(" || ", $member_data); if($col_data[0] == $number) { // 내용을 보여줄 레코드이면 반복문을 빠져나감 $name = $col_data[1]; $id = $col_data[2]; $address = $col_data[3]; $phone = $col_data[4]; $handphone = $col_data[5]; $email = $col_data[6]; $homepage = $col_data[7]; $date = $col_data[8]; $count = $col_data[9]; break; } } ?> <TABLE width=600 align=center> <TR><td align=center> <font size=3><b>[ 회원 관리 - 수정 ]</b></font> </td></TR> </TABLE> <br> <TABLE width=600 align=center border=1> <FORM METHOD=POST ACTION=""><br> <TR> <TD width=150 align=center><font size=2>이름</TD> <TD align=center><INPUT TYPE="text" NAME="name" value=<?echo("$name");?>></TD> </tr> <TR> <TD width=150 align=center><font size=2>ID</TD> <TD align=center><INPUT TYPE="text" NAME="id" value=<?echo("$id");?>></TD> </tr> <TR> <TD width=150 align=center><font size=2>주소</TD> <TD align=center><INPUT TYPE="text" NAME="address" value=<?echo("$address");?>></TD> </tr> <TR> <TD width=150 align=center><font size=2>전화</TD> <TD align=center><INPUT TYPE="text" NAME="phone" value=<?echo("$phone");?>></TD> </tr> <TR> <TD width=150 align=center><font size=2>휴대폰</TD> <TD align=center><INPUT TYPE="text" NAME="handphone" value=<?echo("$handphone");?>></TD> </tr> <TR> <TD width=150 align=center><font size=2>E-Mail</TD> <TD align=center><INPUT TYPE="text" NAME="email" value=<?echo("$email");?>></TD> </tr> <TR> <TD width=150 align=center><font size=2>Homepage</TD> <TD align=center><INPUT TYPE="text" NAME="homepage" value=<?echo("$homepage");?>></TD> </tr> <TR> <TD align=center colspan=2><INPUT TYPE="submit" value=" 수정 합니다 "></TD> </tr> <INPUT TYPE="hidden" Name="mode" value="process"> <INPUT TYPE="hidden" Name="number" value="<?echo("$number");?>"> </FORM> </table> <? } ?> </BODY> </HTML> 예제 5 : 회원관리 - 삭제하기(delete.html)
<HTML> <HEAD> <TITLE> Leelab - 파일 처리(2)</TITLE> </HEAD> <BODY BGCOLOR="#FFFFFF"> <? if($number == "") { echo ("<meta http-equiv='Refresh' content='3; URL=list.html'>"); exit; }
$memberfile = "./member_file.data";
if(!strcmp($mode, "process")) {
$file_size = filesize($memberfile); if($fp = fopen($memberfile, 'r')) { // 내용 읽어오기 $member_file_data = fread($fp, $file_size); fclose($fp); } else { echo("${memberfile}을 여는데 실패했습니다."); exit; }
$member_line = explode("\n", $member_file_data); // 각 레코드별로 파싱 if($fp = fopen($memberfile, 'w')) {
$no = 0; while($member_data = $member_line[$no++]) { // 각 레코드를 검사 $col_member_data = explode(" || ", $member_data); if($col_member_data[0] == $number) { // 삭제할 레코드이면 $name = $col_member_data[1]; $address = $col_member_data[2]; $phone = $col_member_data[3]; continue; }else { fwrite($fp, $member_data, strlen($member_data)); fwrite($fp, "\n", strlen("\n")); } } } else { echo("${memberfile}을 여는데 실패했습니다."); exit; }
echo("<br>삭제하였습니다.<br> 잠시 기다리시면 목록화면으로 갑니다.<BR>\n"); echo("이름 : $name <BR>\n"); echo("주소 : $address <BR>\n"); echo("전화 : $phone <BR>\n"); echo ("<meta http-equiv='Refresh' content='3; URL=list.html'>");
}else {
$file_size = filesize($memberfile); if($fp = fopen($memberfile, 'r')) { // 내용 읽어오기 $member_file_data = fread($fp, $file_size); fclose($fp); } else { echo("${memberfile}을 여는데 실패했습니다."); exit; }
$member_line = explode("\n", $member_file_data); // 각 레코드별로 파싱
$no = 0; while($member_data = $member_line[$no++]) { // 각 레코드를 검사 $col_data = explode(" || ", $member_data); if($col_data[0] == $number) { // 내용을 보여줄 레코드이면 반복문을 빠져나감 $name = $col_data[1]; $id = $col_data[2]; $address = $col_data[3]; $phone = $col_data[4]; $handphone = $col_data[5]; $email = $col_data[6]; $homepage = $col_data[7]; $date = $col_data[8]; $count = $col_data[9]; break; } }
?> <TABLE width=600 align=center> <TR><td align=center> <font size=3><b>[ 회원 관리 - 삭제 ]</b></font> </td></TR> </TABLE> <br> <TABLE width=600 align=center border=1> <FORM METHOD=POST ACTION=""><br> <TR> <TD width=150 align=center><font size=2>이름</TD> <TD align=center><?echo("$name");?></TD> </tr> <TR> <TD width=150 align=center><font size=2>ID</TD> <TD align=center><?echo("$id");?></TD> </tr> <TR> <TD width=150 align=center><font size=2>주소</TD> <TD align=center><?echo("$address");?></TD> </tr> <TR> <TD width=150 align=center><font size=2>전화</TD> <TD align=center><?echo("$phone");?></TD> </tr> <TR> <TD width=150 align=center><font size=2>휴대폰</TD> <TD align=center><?echo("$handphone");?></TD> </tr> <TR> <TD width=150 align=center><font size=2>E-Mail</TD> <TD align=center><?echo("$email");?></TD> </tr> <TR> <TD width=150 align=center><font size=2>Homepage</TD> <TD align=center><?echo("$homepage");?></TD> </tr> <TR> <TD align=center colspan=2><INPUT TYPE="submit" value=" 삭제 합니다 "></TD> </tr> <INPUT TYPE="hidden" Name="mode" value="process"> <INPUT TYPE="hidden" Name="number" value="<?echo("$number");?>"> </FORM> </table> <? } ?> </BODY> </HTML>
|
[목차] |