23. 라이브러리 1

https://www.opentutorials.org/course/1688/9480



1) 라이브러리 1 : 개론

지금까지 살펴본 기술의 발전 방향은? 중복의 제거


중복의 제거로 인한 효과

- 유지보수의 편의성

- 코드의 양 감소

- 가독성 향상


소프트웨어에 있어서 코드란? 

- 코드 == 설계도 (false)

  코드 == 제품 (true)  → 코드를 작성하는 것이 곧 제품을 만드는 것이다.


코드의 중복 제거 → 생산성의 향상


'중복을 제거한다' 와 '제거한 코드를 여러 곳에서 재사용한다' 라는 것은 뗄레야 뗄 수 없는 관계

- 중복 제거 ↔ 재사용


프로그래밍에서 라이브러리란?

- 중복해서 사용되는 로직을 재사용 할 수 있도록 부품화(모듈화) 시킨 것


라이브러리의 사용

- 하나의 프로젝트에서 재사용

- 자신의 프로젝트에서 재사용

- 모두의 프로젝트에서 재사용



2) 라이브러리 2 : 직접 만들기


/lib/db.php

1
2
3
4
<?php
  $conn = mysqli_connect('localhost''root''');
  mysqli_select_db($conn'opentutorials');
?>
cs

- 중복을 제거한 수준


1
2
3
4
5
6
7
<?php
    function db_init($host$duser$dpw$dname){
          $conn = mysqli_connect($host$duser$dpw);
          mysqli_select_db($conn$dname);
          return $conn;
    }
?>
cs

- 재활용성을 높이도록 수정


/config/config.php

1
2
3
4
5
6
7
8
<?php
$config = array(
    "host" => "localhost",
    "duser"=>"root",
      "dpw"=>"111111",
      "dname"=>"opentutorials"
);
?>
cs


index.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?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="/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="/index.php">JavaScript</a></h1>
    </header>
    <nav>
      <ol>
      <?php
        while($row=mysqli_fetch_assoc($result)) {
          echo '<li><a href="/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="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>
cs


write.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?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="/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="/index.php">JavaScript</a></h1>
    </header>
    <nav>
      <ol>
      <?php
        while($row=mysqli_fetch_assoc($result)) {
          echo '<li><a href="/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="write.php">쓰기</a
    </div>
    <article>
      <form action="/process.php" method="post">
        <p>제목 : <input typt="text" name="title"></p>
        <p>작성자 : <input typt="text" name="author"></p>
        <p>본문 : <textarea name="description"></textarea>
        <input type="submit" name="name">
      </form>
    </article>
  </body>
</html>
cs


process.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?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');
?>
cs






+ Recent posts