So here's an example code I use to show all the categories that are linked to a certain postID. For example post 1 has the categories: Apple, Green and Yellow linked to it.
But I can't seem to fetch the data correctly since it has already been fetched once at the top i can't do a proper while loop at the Categories:
part of my code where I try to do a while loop. The while loop works and fetches all the categories except the First one and also when I place the while loop the
$row['postTitle']
and
$row['postCont']
won't appear anymore because it's being skipped. How would I fix something like this? Thanks.
<?php require('includes/config.php');
$stmt = $db->prepare(" SELECT *
FROM blog_posts
LEFT JOIN blog_posts_categories ON blog_posts.postID=blog_posts_categories.postID
INNER JOIN blog_categories ON blog_posts_categories.catID=blog_categories.catID
WHERE blog_posts.postID = :postID");
$stmt->execute(array(':postID' => $_GET['id']));
$row = $stmt->fetch();
//if post does not exists redirect user to homepage.
if($row['postID'] == ''){
header('Location: ./');
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title><?php echo $row['postTitle'];?> | Website</title>
<link rel="stylesheet" href="style/normalize.css">
<link rel="stylesheet" href="style/main.css">
</head>
<body>
<div id="wrapper">
<h1>Single Post Page</h1>
<hr />
<p><a href="./">Home</a> |
Categories:
<?php while($row = $stmt->fetch())
{
//the first category is being skipped? How to fix?
echo $row['catName'];
} ?>
</p>
<div>
<?php
//these won't appear because of the while loop. It's being skipped.
echo '<h1>'.$row['postTitle'].'</h1>';
echo '<p>'.$row['postCont'].'</p>';
?>
</div>
</div>
</body>
</html>