Cannot get php/mysql to display single page of item from db

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:


     * 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);


     * 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());    
            return $stmt->fetch();
            if($row['id'] == ''){
                header('Location: ./');

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

so what does getBlog() do? Did you try having a look at any variable involved?

Also this looks wrong $id ='id' as you set a variable to contain a string, rather then using an integer.

You can test the regexes for your router here:

Hi chorn, thanx for your reply.
so what does getBlog() do? It was supposed to get a single blog post (the one being clicked on).
I am trying to create working readmore link to the full post. I have downloaded tutorials and videos but somehow this is still beating me

Sponsor our Newsletter | Privacy Policy | Terms of Service