PHP 반복문과 조건문 활용하기
PHP

PHP 반복문과 조건문 활용하기

by nocagklhw 2019. 12. 28.

scandir는 폴더에 있는 파일을 스캔하는 함수이다. 

 

설명은 코드에

<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>WEB</title>
</head>
<body>
    <h1><a href="index.php">WEB</a></h1>
    <ol>
        <?php 
            $list = scandir('./data');
            $i = 0;
            while($i < count($list)) {
                if($list[$i] != '.') {
                    if($list[$i] != '..') {
                    echo "<a href=\"index.php?id=$list[$i]\"><li>$list[$i]</li></a>\n";
                    }
                } //if문에서 '.'과 '..'을 빼는 이유는 scandir은 '.'과 '..'이 배열의 첫번째와 두번째에 추가되기 때문이다. 사실 if를 사용하지 않아도 14번 행에서 i를 2로 지정하면 if를 쓸 이유가 없어지지만 활용을 위해 쓴 것이다. 
                $i = $i + 1;
            }
        ?>
    <h2>
        <?php if(isset($_GET['id'])) {
            echo $_GET['id'];
        } else {
            echo "Welcome";
        }
        ?>
    </h2>
    </ol>
    <h2>
        <?php
        if(isset($_GET['id'])) {
        echo file_get_contents("data/".$_GET['id']);
        } else {
            echo "hihihihi";
        }
        ?>
    </h2>
</body>
</html>

댓글


TOP

TEL. 02.1234.5678 / 경기 성남시 분당구 판교역로