Hello everyone.
I am new here. And new to php MVC (OOP).
I have a page that displays a list of blog posts from the database. All I want is each title to have a hyperlink that takes you to the full page of that particular post. I am using PHP, Mysql and Twig templating engine. Here are my current codes:
Blogs.php:
/**
* Display a single Blog post
*
* @return void
*/
public function showAction() {
$blog = BlogsModel::getBlog($id ='id');
$data = [
'blog' => $blog
];
View::renderTemplate("Services/Blogs/show.php", $data);
}
BlogsModel.php:
/**
* Show single blog post with writer info
*/
public static function getBlog($id) {
$sql = 'SELECT *,
id as id,
user_id as user_id
FROM blogs
INNER JOIN users
ON blogs.user_id = user.user_id
WHERE status="published" AND id=:id
';
$db = static::getDB();
$stmt = $db->prepare($sql);
$stmt->bindValue(':id', $id, PDO::PARAM_INT);
$stmt->setFetchMode(PDO::FETCH_CLASS, get_called_class());
$stmt->execute();
return $stmt->fetch();
if($row['id'] == ''){
header('Location: ./');
exit;
}
}
blogsAll.php: (displays list of posts, here is ReadMore hyperlink)
<a id="readMore" href="/blogs/{{ blog.slug }}" class="btn btn-success">ReadMore</a>
index.php: (front controller)
$router->add('blogs/[\w-]+', ['controller' => 'Blogs', 'action' => 'show']);
Upon being clicked, I get the correct slug in the url (which is what I want), e.g. http://localhost/blogs/test-blog-2
But then it displays only Test Blog 1. If i click Test Blog 4, it shows Test Blog 1. And so on. Alaways just shows Test Blog 1.
Please help. I’m totally stumped. Thanx in advance