sidekick Posted November 24, 2020 Share Posted November 24, 2020 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> Quote Link to comment https://forums.phpfreaks.com/topic/311764-display-single-blog-post-from-db-in-php-mvc/ Share on other sites More sharing options...
NotionCommotion Posted November 25, 2020 Share Posted November 25, 2020 (edited) 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 November 25, 2020 by NotionCommotion 1 Quote Link to comment https://forums.phpfreaks.com/topic/311764-display-single-blog-post-from-db-in-php-mvc/#findComment-1582640 Share on other sites More sharing options...
Barand Posted November 25, 2020 Share Posted November 25, 2020 NOTE: both instances of $db->query(..) in the above post should be $db->prepare(..) 1 1 Quote Link to comment https://forums.phpfreaks.com/topic/311764-display-single-blog-post-from-db-in-php-mvc/#findComment-1582643 Share on other sites More sharing options...
sidekick Posted November 25, 2020 Author Share Posted November 25, 2020 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 Quote Link to comment https://forums.phpfreaks.com/topic/311764-display-single-blog-post-from-db-in-php-mvc/#findComment-1582647 Share on other sites More sharing options...
NotionCommotion Posted November 25, 2020 Share Posted November 25, 2020 Post the code you are trying. Also, when doing so, but the code tags (i.e. <>) around them do it appears correct. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/311764-display-single-blog-post-from-db-in-php-mvc/#findComment-1582648 Share on other sites More sharing options...
Strider64 Posted November 26, 2020 Share Posted November 26, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/311764-display-single-blog-post-from-db-in-php-mvc/#findComment-1582649 Share on other sites More sharing options...
sidekick Posted December 2, 2020 Author Share Posted December 2, 2020 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 Quote Link to comment https://forums.phpfreaks.com/topic/311764-display-single-blog-post-from-db-in-php-mvc/#findComment-1582753 Share on other sites More sharing options...
sidekick Posted December 2, 2020 Author Share Posted December 2, 2020 @Strider64 Your input looks interesting and I will definitely look into it Quote Link to comment https://forums.phpfreaks.com/topic/311764-display-single-blog-post-from-db-in-php-mvc/#findComment-1582754 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.