PHP 실습
2020. 4. 9. 01:01ㆍ생활코딩/생활코딩웹
개요
여러 가지 중복이 되는 것을 html과 정보를 분리해서 정보를 txt 파일에 넣고 html은 index.php에 넣어주는 것을
실습으로 해보겠습니다
실습
처음에는 토대가 되는 php 파일을 만들어줍니다
<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/phpjs/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>
위의 코딩은 저번의 javascript에서 가져와서 php로 바꾼 코드입니다. 추가로 가져온 것은 생활코딩에서 재공 하는 이미지를 가져왔습니다.
다음으로 php의 안에 file_get_contents을 사용하는데 이것은 지정한 파일을 가져오라고 하는 기능입니다.
그리고 get을 사용하여 "[파일의 이름]. txt"을 id로 지정된 것을 가져옵니다.
다음으로 id값을 넣을 txt 파일을 확인해보겠습니다.
1.txt
<h2>JavaScript란?</h2>
JavaScript는 html을 제어합니다.
2.txt
<h2>변수와 상수</h2>
변수는 바뀌는 것 상수는 바뀌지 않은 것
3.txt
<h2>연산자</h2>
연산자는 계산하는 것입니다
위처럼 txt 파일을 각각 3개 만들어줍니다.
다음 파일을 txt 파일을 가져오기 위해 list파일을 만들어 표현할 것입니다.
<li><a href="http://localhost/phpjs/index.php?id=1">JavaScript란?</a></li>
<li><a href="http://localhost/phpjs/index.php?id=2">변수와 상수</a></li>
<li><a href="http://localhost/phpjs/index.php?id=3">연산자</a></li>
이렇게 list.txt를 만들어주면 위에서 get으로 지정된 값을 id를 각각 지정해줍니다 그러면 다시 반환되어 값이 표현됩니다.
위의 사진처럼 나오는 것을 볼 수 있습니다.
참고한 수업 링크
'생활코딩 > 생활코딩웹' 카테고리의 다른 글
MySQL 실습 (0) | 2020.04.09 |
---|---|
데이터베이스(MySQL) 이론 (0) | 2020.04.09 |
자바스크립트 실습 (0) | 2020.04.08 |
프로그래밍 접근방법 (0) | 2020.04.07 |
UI vs API (0) | 2020.04.07 |