17. PHP실습

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


1) PHP 실습1


- 하나의 PHP 안에 HTML 코드들이 모여있고, 정보들은 각각의 텍스트 파일로 분리되어 있는 상태


 데이터베이스의 역할

- 체계적인 데이터 관리

- 많은 데이터를 효과적으로 처리

- 높은 보안성



2) PHP 실습2


정보는 파일에 저장하고, HTML은 PHP 안에서 관리하도록 개선시켜보자


1.php

1
2
3
<?php
echo $_GET['name'].",".$_GET['id'];
?>
cs


- ? : 주소와 값 구분

- & : 값과 값 구분


2.php

1
2
3
4
5
6
7
8
9
10
<html>
  <head>
    <title></title>
  </head>
  <body>
    <?php
      echo file_get_contents($_GET['id'].".txt"); // id 값에 따라 파일 불러옴
    ?>
  </body>
</html>
cs


1.txt

1
coding everybody!
cs




3) PHP 실습3


문서에서 정보와 정보가 아닌 부분을 분리한 후, 정보들을 txt 파일로 빼내자


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
<!DOCTYPE html>
<html>
  <head>
    <meta charset = "utf-8">
    <link rel="stylesheet" type="text/css" href="/style.css">
  </head>
  <body id="target">
    <header>
      <h1><a href="/">JavaScript</a></h1>
    </header>
    <nav>
      <ol>
        <?php
          echo file_get_contents("list.txt")
        ?>
      </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'"/>
    </div>
    <article>
      <h2>JavaScript란?</h2>
      JavaScript는 웹페이지를 프로그래밍적으로 제어하는 언어입니다.
    </article>
  </body>
</html>
 
cs


list.txt

1
2
3
<li><a href="/page_html.html">JavaScript란?</a></li>
<li><a href="/page_vc.html">변수와 상수</a></li>
<li><a href="/page_op.html">연산자</a></li>
cs



4) PHP 실습4

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
<!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
          echo file_get_contents("list.txt")
        ?>
      </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'"/>
    </div>
    <article>
      <?php
      if(empty($_GET['id']) == false) {
        echo file_get_contents($_GET['id'].".txt");
      }
      ?>
    </article>
  </body>
</html>
cs


list.txt

1
2
3
<li><a href="/index.php?id=1">JavaScript란?</a></li>
<li><a href="/index.php?id=2">변수와 상수</a></li>
<li><a href="/index.php?id=3">연산자</a></li>
cs


1.txt

1
2
<h2>JavaScript란?</h2>
JavaScript는 웹페이지를 프로그래밍적으로 제어하는 언어입니다.
cs


2.txt

1
2
<h2>변수와 상수</h2>
변수는 바뀔 수 있는 값이고, 상수는 바뀌지 않는 값입니다.
cs


3.txt

1
2
<h2>연산자</h2>
계산을 할 때 사용되는 것입니다.
cs


style.css - 추가부분

1
2
3
4
header img{
  float:right;
  height:100px; 
}
cs




+ Recent posts