Member Avatar for Member #798579

Hey.

I've have been attempting to create a website and I was just wondering something.

On my page's I have post's like this..

<h3>This Post</h3>
<p>Content of post</p>

How do you make it so when I click the title, it will show just that post and then if I want it to, Also show a comment box underneath.
A bit like Tumblr or Wordpress, So when you click the post title, it shows that post plus a comment box.

The only way I can think of is using

<?php include("filename.php")?>

but wouldn't that mean seperate .php files for each post?

Thanks in advance.

Dani AI

Generated

As suggested, organize your views (list vs. single post). A common, lightweight pattern is to keep one "post" template that loads the requested entry by ID or slug, shows the content, and displays a comment form. That avoids making a separate PHP file per post and keeps routing and rendering simple.

Minimal server-side workflow (safe defaults: prepared statements, output escaping, CSRF token, POST-redirect-get):

<?php
require 'config.php'; // sets up $pdo
session_start();

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['post_id'], $_POST['comment'], $_POST['csrf'])) {
    if (!hash_equals($_SESSION['csrf'] ?? '', $_POST['csrf'])) { http_response_code(400); exit('Invalid request'); }
    $postId = (int) $_POST['post_id'];
    $comment = trim($_POST['comment']);
    if ($comment !== '') {
        $stmt = $pdo->prepare('INSERT INTO comments (post_id, body, created_at) VALUES (?, ?, NOW())');
        $stmt->execute([$postId, $comment]);
        header('Location: /post.php?id=' . $postId); exit;
    }
}

$id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
$stmt = $pdo->prepare('SELECT id, title, body FROM posts WHERE id = ? LIMIT 1');
$stmt->execute([$id]);
$post = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$post) { http_response_code(404); echo 'Post not found'; exit; }

echo '<h1>' . htmlspecialchars($post['title']) . '</h1>';
echo '<div>' . nl2br(htmlspecialchars($post['body'])) . '</div>';

$_SESSION['csrf'] = bin2hex(random_bytes(16));
?>
<form method="post">
<input type="hidden" name="post_id" value="<?php echo (int)$post['id']; ?>">
<input type="hidden" name="csrf" value="<?php echo $_SESSION['csrf']; ?>">
<textarea name="comment"></textarea>
<button type="submit">Post comment</button>
</form>

Pretty URLs are optional. A simple rewrite example for Apache:

RewriteEngine On
RewriteRule ^post/([0-9]+)/?$ post.php?id=$1 [L,QSA]

Quick cautions: always use parameterized queries, escape output with htmlspecialchars, limit comment size, and add spam controls (honeypot, rate limit, CAPTCHA). For XSS and input safety see OWASP guidance and for prepared statements see the PHP PDO docs (OWASP XSS, PDO prepared statements).

Recommended Answers

All 5 Replies

Yes, separate your different 'views' into different php files, because that's essentially what you want. When you click the title of a post you want to go to it's standalone 'view' or 'page' with just the post on it, with a comment box as well. What you can do is do a php check to see if the user is on the main page when clicking the link, if they are, then take them to the view that you want (which would be a singular post with a comment box).

Member Avatar for Member #798579

How would that be done?

Do you have any sample code?


Thanks.

I would set it up similar to the way wordpress does things. It's very orginized that way. Look through the hierarchy here to get a basic overview of how to arrange your php files. Code samples can also be found there.

Member Avatar for Member #334542

Wordpress gives you all the options in their configuration panel to display or hide the comment boxes as the part of CMS.

Member Avatar for Member #798579

I've already managed to sort it.

Thanks for the reply.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.