본문 바로가기
Coding/WebApp

PHP 실습

by 그냥그렇듯이 2017. 8. 26.
반응형

모든 저작권은 <생활코딩>의 생산자인 <egoing>님에게 있습니다.

문제시, 비공개로 전환하겠습니다.


<PHP 실습>

- 요청 -
웹브라우저 -> http://a.com/a.php -> 웹서버 -> php엔진 -> File

- 응답 -
웹브라우저 <- http://a.com/a.php <- 웹서버 -> php엔진 <- File

- html과 정보의 분리 -
html: index.php
정보: 다양한 txt파일에 담는다. => DATABASE(MySQL)
웹브라우저 -> a.com/index.php?id=1 -> index.php <- DATABASE

 예제 1)

 <?php

echo $_GET['name'],",",$_GET['id'];

 ?>


주소와 주소를 구분할때는 ?를 쓴다.
값과 값을 구분하는데는 &를 쓴다.

 2.php

 <!DOCTYPE html>

<html>

  <head>

    <meta charset="utf-8">

  </head>

  <body>

    <?php

    echo file_get_contents("1.txt");

    ?>


  </body>

</html>

 1.txt

 coding everybody

 

 2.php 수정

 <!DOCTYPE html>

<html>

  <head>

    <meta charset="utf-8">

  </head>

  <body>

    <?php

    echo file_get_contents($_GET['id'].".txt");

    ?>


  </body>

</html>

 


 2.txt

 Hello World

 

index.php 

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

      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>

 list.txt

 <li><a href="http://localhost/index.php?id=1">JavaScript란?</a></li>

<li><a href="http://localhost/index.php?id=2">변수와 상수</a></li>

<li><a href="http://localhost/index.php?id=3">연산자</a></li>

 1.txt

 <h2>JavaScript란?</h2>

JavaScript는 html을 제어합니다.

 2.txt

 <h2>변수와 상수</h2>

변수는 바뀌는 것 상수는 바뀌지 않은 것

 3.txt

 <h2>연산자</h2>

연산자는 계산하는 것입니다

 



댓글