Fedora + DB

Fedora DB 게시글 보기

OnejinSim 2023. 12. 18. 17:27

데이터 베이스와 연동된 게시글 목록 페이지에서 하나의 게시글을 클릭하면

해당 게시글의 페이지로 넘어가 자세한 내용을 볼 수 있도록 해보자.

 

페이지 간의 이동을 하기 위해서 a태그를 사용해 볼건데 특정 게시글을 보기 위해 특정한 값을 주고 받을 수 있어야한다.

이를 위해서

<a href="detail.php?index=<?php echo $row['id']; ?>">

와 같은 방식으로 값을 넘겨줄 수 있다. detail.php 파일에서 

$index = $_GET['index'];

와 같은 방식으로 값을 넘겨 받을 수 있다.

 

vi /var/www/html/board.php

<table border=1>
 <tr>
  <th>ID</th>
  <th>UserID</th>
  <th>Title</th>
  <th>Content</th>
  <th>Writer</th>
  <th>Create Date</th>
  <th>Reply ID</th>
 </tr>

<?php
        include "dbconn.php";

        session_start();
        echo "<p>Current User ID: " . $_SESSION["uname"] . "</p>";

        $result = mysqli_query($conn, "select * from tbBlog");
        $data = $result -> fetch_all(MYSQLI_ASSOC);

        foreach($data as $row):
?>

 <tr>
  <td><?= $row['id'] ?></td>
  <td><?= $row['tbUserID'] ?></td>
  <td><a href="detail.php?index=<?php echo $row['id']; ?>"><?= $row['title'] ?></a></td>
  <td><?= $row['content'] ?></td>
  <td><?= $row['writer'] ?></td>
  <td><?= $row['create_date'] ?></td>
  <td><?= $row['replyID'] ?></td>
 </tr>

<?php endforeach ?>
</table>

이렇게 되면 각 Title 마다 DB와 연동된 특정 id값을 가지고 넘겨줄 수 있게된다.

 

 

좌: board.php / 우: detail.php

 

 

 

넘겨받은 detail.php에서는 이 값을 DB에 적용해서 원하는 게시글의 정보를 받아온다.

vi /var/www/html/detail.php

<?php

  include "dbconn.php";
  session_start();
  $index = $_GET['index'];


  $select = "select * from tbBlog where id = '$index'";
  $result = mysqli_query($conn, $select);

  while($row = mysqli_fetch_assoc($result)){
     $title = $row['title']; $content = $row['content']; $writer = $row['tbUserID'];
  }

?>


<h3>Title: <?= $title ?></h3>
<p>Writer: <?= $writer ?></p>
<hr></hr>
<p>content: <?= $content ?></p>

<button onclick='history.back()'>Back</button>

 

원하는 값을 불러왔으면 html코드를 통해 화면에 출력해준다.