I'm building a blog and I'm done with the webpage which shows all the posts with their titles, pictures and short descriptions (each post = one picture, one title and one description). Next I want to enable redirecting users to a page with a full post after clicking a chosen title, but I don't know where to start.
home.php
<?php
session_start();
require_once('connection.php');
mysql_select_db($database_connection, $connection);
$query_post = "SELECT postID, title, date, category, description, pimage, text FROM post";
$post = mysql_query($query_post, $connection) or die(mysql_error());
$row_post = mysql_fetch_assoc($post);
$totalRows_post = mysql_num_rows($post);
mysql_select_db($database_connection, $connection);
$query_joining = "SELECT * FROM image inner join post on post.pimage = image.imagename";
$joining = mysql_query($query_joining, $connection) or die(mysql_error());
$row_joining = mysql_fetch_assoc($joining);
$totalRows_joining = mysql_num_rows($joining);
?>
<ul>
<?php
while($row_joining = mysql_fetch_assoc($joining)) {
?>
<li>
<a href="post.php"><img src="images/<?php echo $row_joining['imagename']; ?>"></a>
<h3><a href="post.php"><?php echo $row_joining['title']; ?></a></h3>
<p><?php echo $row_joining['description']; ?></p>
<a href="post.php">Read more</a>
</li>
<?php
}
?>
</ul>
post.php
<div>
<h1><?php echo $row_post['title']; ?></h1>
<img "images/<?php echo $row_post['pimage']; ?>">
<h4><?php echo $row_post['date']; ?>, <?php echo $row_post['category']; ?></h4>
<p><?php echo $row_post['text']; ?></p>
</div>
I know that especially the post.php
needs changes, but I don't know how to proceed so that the chosen post will be displayed in post.php
.
When you showing all posts, in the href you can put like this:
<a href="post.php?post='.$row_post['postID'].'">
Then in post.php you can do something like:
if (isset($_GET['post']))
{
// check if post is integer
// check if post exist in database
// show content from that post only. WHERE postID = data
}
else
{
// list all posts.
}