Jump to content

Display single blog post from db in php mvc


sidekick

Recommended Posts

Hello everyone.

I'm new here and to php mvc. Been trying to hone my skills with mvc, mysql, classes and functions. But I am stuck. Can display all blog posts from DB but not a single post. The Readmore link only leads to post 1 (or the next published post from the db). Please I need your help. My code is below. Thanx in advance.

BlogsModel.php (PDO db):

public static function getBlog($blogsID) {

try {

$db = static::getDB();

$stmt = $db->query('SELECT *

FROM blogs

WHERE blogsStatus=1 AND blogsID=blogsID

');

$result = $stmt->fetch(PDO::FETCH_ASSOC);

return $result;

} catch (PDOException $e) {

echo $e->getMessage();

}

}

Blogs.php (Controller):

public function blogAction() {

$blog = BlogsModel::getBlog($blogsID = 'blogsID');

$user = BlogsModel::getBlog($blogsUserID = 'blogsUserID');

$data = [

'blog' => $blog,

'user' => $user

];

View::renderTemplate("Services/Blogs/blog.php", $data);

}

index.php (front controller):

$router->add('blogs/{id:[\w-]+}', ['controller' => 'Services\Blogs', 'action' => 'show']);

ReadMore link:

<a id="readMore" href="/blogs/{{ blog.blogsAlias }}" >ReadMore</a>

 

Link to comment
Share on other sites

plogsID=plogsID will always be true.

SELECT * FROM blogs WHERE blogsStatus=1 AND blogsID=blogsID

You have several options.  See https://www.php.net/manual/en/pdo.prepare.php, but to get you started...

$stmt = $db->query('SELECT * FROM blogs WHERE blogsStatus=1 AND blogsID=:blogsID');
$stmt->execute(['blogsID' => $blogsID]);
$result = $stmt->fetch(PDO::FETCH_ASSOC);

 

$stmt = $db->query('SELECT * FROM blogs WHERE blogsStatus=1 AND blogsID=?');
$stmt->execute([$blogsID]);
$result = $stmt->fetch(PDO::FETCH_ASSOC);

Or you can use use bindParam() or bindValue(), but don't worry about that for now. 

Edited by NotionCommotion
  • Like 1
Link to comment
Share on other sites

Hi NotionCommotion and Barand

Thank you very much for your speedy and helpful replies. I made changes as requested above, however, I had to change 'query' to 'prepare' as it threw an error. I tried both above and they both gave me a blank page.

I did a var_dump() on the routes and this is what I get:

array(1) { ["blogs/test-blog-4"]=> string(0) "" }
array(23) { ["/^(?P[a-z-]+)\/(?P[a-z-]+)$/i"]=> array(0) { }
["/^blogs\/(?P[\w-]+)$/i"]=> array(2) { ["controller"]=> string(14) "Services\Blogs" ["action"]=> string(4) "blog" }

I had clicked on the ReadMore link for Test Blog 4. The slug of the blog post is correct in the url but nothing shows.

Thanx again

Link to comment
Share on other sites

Personally I just redirect to another php file if a certain about of text should only be displayed. I do it like the following:

    public function getIntro($content = "", $count = 200, $pageId = 0) {;
        $this->content = (string) $content;
        return substr($this->content, 0, (int)$count) . '<a class="moreBtn" href="mainArticle.php?page=' . (int)$pageId . '"> ...more</a>';
    }

If I want to have is stay on the same page I simply would just introduce vanilla javascript. (Which I might after I get done overhauling my website)

I just simply display a page like this

    public function page($id) {
        $this->query = 'SELECT id, user_id, author, page, post, page, thumb_path, path, Model, ExposureTime, Aperture, ISO, FocalLength, heading, content, DATE_FORMAT(date_added, "%W, %M %e, %Y") as date_added, date_added as myDate FROM cms WHERE id=:id';
        $this->stmt = static::pdo()->prepare($this->query);
        $this->stmt->execute([':id' => $id]);
        $this->result = $this->stmt->fetch(PDO::FETCH_OBJ);
        return $this->result;
    }

It doesn't matter what page the class is on, just as long as it has the id (page id) of the database table record. 

I use pagination for control multiple records that I want to display on a website page. Which is relatively easy to do/setup and something that you might look into if you are develop a blog. I used to try to setup a MVC, but found it more of a hassle than what it was worth when developing a simple CMS/Blog page. 

Link to comment
Share on other sites

Hi guys

Been busy with another project but am back to look further into this. Here is the current code as requested by NotionCommotion.

BlogsModel.php

public static function getBlog($id)  {
        try {
            $db = static::getDBUserContent();
            $stmt = $db->prepare('SELECT *
                                FROM blogs
                                WHERE blogsStatus=1 AND id=:id
                                ');

            $stmt->bindValue(':id', $id, PDO::PARAM_INT);
            
            $stmt->execute([':id' => $id]);
    
            $result = $stmt->fetch(PDO::FETCH_ASSOC);
    
            return $result;

Blogs.php (controller)

public function blogAction() {
        $blog = BlogsModel::getBlog($id = 'id');
    
        $data = [
            'blog' => $blog,
            'user' => $user
        ];

Index.php (front controller)

$router->add('blogs/{id:[\w-]+}', ['controller' => 'Services\Blogs', 'action' => 'blog']);

Thanx again

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.