게시판 만들기 (Index.php)

황인환·2024년 5월 12일

index.php

<?php
session_start();
if($_COOKIE['normal']=="ok"){
    function board(){
        $conn=mysqli_connect('localhost','root','12345678a','text_db');
        $sql="SELECT * FROM board ORDER BY idx ASC";
        $result=mysqli_query($conn,$sql);
        while($row=mysqli_fetch_array($result)){
            echo 
            "<tr>".
            "<td>".$row['idx']."</td>".
            "<td>"."<a href='detail.php?idx=$row[idx]'>".$row['title']."</a>"."</td>".
            "<td>".$row['author']."</td>".
            "<td>".$row['content']."</td>".
            "<td>".$row['hit']."</td>".
            "<td>".$row['date']."</td>".
            "<td>".$row['ip']."</td>".
            "<td>"."<a href=\"modify.php?idx=$row[idx]\" onclick=\"return confirm('really')\">"."Modify"."</td>".
            "<td>"."<a href=\"delete.php?idx=$row[idx]\" onclick=\"return confirm('really')\">"."Delete"."</td>".
            "</tr>";
        }
    }
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
    
    </head>
    <body>
        <table width="500" border="1">
            <tr>
                <td colspan="9" >Board</td>
            </tr>
            <tr>
                <td>idx</td>
                <td>title</td>
                <td>author</td>
                <td>content</td>
                <td>hit</td>                
                <td>date</td>                
                <td>ip</td>
                <td>modify</td>
                <td>delete</td>
            </tr>
            <?=board();?>
            <tr>
                <td colspan="9">
                    <input type="button" value="Create" onclick="location.href='create.php'">
                    <input type="button" value="Mypage" onclick="location.href='mypage.php'">
                    <input type="button" value="Logout" onclick="location.href='logout.php'">
                </td>
            </tr>
        </table>

    </body>
</html>
<?php
}else{
    header("Location: login.php");
}
?>

1.if($_COOKIE['normal']=="ok") 이 코드는 로그인에 성공하면 쿠키를 발급하는데 쿠키를 가지고 있지 않다면 마지막 줄에}else{header("Location: login.php?fail=noExist");}를 통해서 다시 로그인페이지로 이동하게 된다.

  1. function board는 아래 table 태그안에 내용을 db에서 끌어오기 위해 만든 것이며 안에 while($row=mysqli_fetch_array($result)) 코드는 while문의 특성상 True값이 아닐때까지 계속하며 위에 코드는 DB에서 row가 없어지면 null값이 나와 자동으로 반복을 멈춘다. 따라서 화면에 전체열이 표시된다.

  2. <a href=\"modify.php?idx=$row[idx]\" onclick=\"return confirm('really')\"> function board 안이 이 코드는 버튼을 누르면 경고창이 뜨면서 다른페이지로 이동하고 싶은 2가지 이벤트(onclick)를 발생시키고 싶었다. 함수를 만들면 코드가 너무 길어져 a태그로 정하였다.
    $row[idx]는 수정이나 삭제할때 맞는 위치를 찾기위해 URL값에 GET방식으로 설정하였다.

  1. function board 아래 input태그들은 각페이지로 이동하기위해 만들었으며 a태그를 넣어도 되지만 버튼 형태를 원해서 input태그에 button타입으로 했다.

--Normaltic Study 4주차 4일--

0개의 댓글