모든 저작권은 <생활코딩>의 생산자인 <egoing>님에게 있습니다.
문제시, 비공개로 전환하겠습니다.
<라이브러리1 (직접만들기)>
기술의 발전 방향 = 중복의 제거
- 이점 -
1. 유지보수 편의
2. 속도 향상
3. 가독성 향상
소프트웨어에 있어서 코드란?
코드 == 설계도
코드 == 제품
프로그램에게 library란?
중복해서 사용되는 로직을 재사용할 수 있도록 부품화(모듈화)시킨 것.
Lib은 하나의 프로젝트에서 재사용되거나 자신의 프로젝트에서 재사용된다.
index.php |
<?php require("config/config.php"); require("lib/db.php"); $conn = db_init($config["host"], $config["duser"], $config["dpw"], $config["dname"]); $result = mysqli_query($conn, "SELECT * FROM topic"); ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <link rel="stylesheet" type="text/css" href="http://localhost/style.css"> </head> <body id="target"> <header> <img src="https://s3.ap-northeast-2.amazonaws.com/opentutorials-user-file/course/94.png" alt="생활코딩"> <h1><a href="http://localhost/index.php">JavaScript</a></h1> </header> <nav> <ol> <?php while( $row = mysqli_fetch_assoc($result)){ echo '<li><a href="http://localhost/index.php?id='.$row['id'].'">'.htmlspecialchars($row['title']).'</a></li>'."\n"; } ?> </ ol> </nav> <div id="control"> <input type="button" value="white" onclick="document.getElementById('target').className='white'"/> <input type="button" value="black" onclick="document.getElementById('target').className='black'" /> <a href="http://localhost/write.php">쓰기</a> </div> <article> <?php if(empty($_GET['id']) === false ) { $sql = "SELECT topic.id,title,name,description FROM topic LEFT JOIN user ON topic.author = user.id WHERE topic.id=".$_GET['id']; $result = mysqli_query($conn, $sql); $row = mysqli_fetch_assoc($result); echo '<h2>'.htmlspecialchars($row['title']).'</h2>'; echo '<p>'.htmlspecialchars($row['name']).'</p>'; echo strip_tags($row['description'], '<a><h1><h2><h3><h4><h5><ul><ol><li>'); } ?> </article> </body> </html> |
lib/db.php |
<?php function db_init($host, $duser, $dpw, $dname){ $conn = mysqli_connect($host, $duser, $dpw); mysqli_select_db($conn, $dname); return $conn; } ?> |
config/config.php |
<?php $config = array( "host"=>"localhost", "duser"=>"root", "dpw"=>"111111", "dname"=>"opentutorials" ); ?> |
write.php |
<?php require("config/config.php"); require("lib/db.php"); $conn = db_init($config["host"], $config["duser"], $config["dpw"], $config["dname"]); $result = mysqli_query($conn, "SELECT * FROM topic"); ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <link rel="stylesheet" type="text/css" href="http://localhost/style.css"> </head> <body id="target"> <header> <img src="https://s3.ap-northeast-2.amazonaws.com/opentutorials-user-file/course/94.png" alt="생활코딩"> <h1><a href="http://localhost/index.php">JavaScript</a></h1> </header> <nav> <ol> <?php while( $row = mysqli_fetch_assoc($result)){ echo '<li><a href="http://localhost/index.php?id='.$row['id'].'">'.$row['title'].'</a></li>'."\n"; } ?> </ ol> </nav> <div id="control"> <input type="button" value="white" onclick="document.getElementById('target').className='white'"/> <input type="button" value="black" onclick="document.getElementById('target').className='black'" /> <a href="http://localhost/write.php">쓰기</a> </div> <article> <form action="process.php" method="post"> <p> 제목 : <input type="text" name="title"> </p> <p> 작성자 : <input type="text" name="author"> </p> <p> 본문 : <textarea name="description"></textarea> </p> <input type="submit" name="name"> </form> </article> </body> </html> |
process.php |
<?php require("config/config.php"); require("lib/db.php"); $conn = db_init($config["host"], $config["duser"], $config["dpw"], $config["dname"]);
$title = mysqli_real_escape_string($conn, $_POST['title']); $author = mysqli_real_escape_string($conn, $_POST['author']); $description = mysqli_real_escape_string($conn, $_POST['description']);
$sql = "SELECT * FROM user WHERE name='".$author."'"; $result = mysqli_query($conn, $sql); if($result->num_rows == 0){ $sql = "INSERT INTO user (name, password) VALUES('".$author."', '111111')"; mysqli_query($conn, $sql); $user_id = mysqli_insert_id($conn); } else { $row = mysqli_fetch_assoc($result); $user_id = $row['id']; } $sql = "INSERT INTO topic (title,description,author,created) VALUES('".$title."', '".$description."', '".$user_id."', now())"; $result = mysqli_query($conn, $sql); header('Location: http://localhost/index.php'); ?> |
댓글