How can PHP be used to display each post on a separate page in a blog system?
To display each post on a separate page in a blog system, you can create a PHP script that retrieves the post content based on a unique identifier (such as post ID) from a database and displays it on a separate page. You can use URL parameters to pass the post ID to the script and dynamically generate the page content.
<?php
// Assuming you have a database connection
$pdo = new PDO('mysql:host=localhost;dbname=blog', 'username', 'password');
// Retrieve post ID from URL parameter
$post_id = $_GET['post_id'];
// Query database for post content
$stmt = $pdo->prepare("SELECT * FROM posts WHERE id = :post_id");
$stmt->bindParam(':post_id', $post_id);
$stmt->execute();
$post = $stmt->fetch();
// Display post content on separate page
echo "<h1>{$post['title']}</h1>";
echo "<p>{$post['content']}</p>";
?>
Keywords
Related Questions
- How can the use of the :: operator impact the functionality of variable variables in PHP?
- What are the potential pitfalls of not thoroughly reviewing the PHP documentation before seeking help in a forum?
- What are the potential benefits of using preg_replace_callback() for string manipulation in PHP?