A simple OOP Comments Display Section

I’ve been teaching myself PHP for over the last couple of months. I have previous programming experience in other languages. I think I have finally grasp programming PHP in the object oriented style. This short tutorial will hopefully show how simple it is to write PHP in the Object Oriented Style. This script will display multiple comments with data coming from a mysqli database. The ones that you that you see where people make comments in the news section or on forums. Like I said it’s a very simple way of displaying formatted multiple comments and feel free to use all of it, modify it or just to look it over to see if you can get the grasp of PHP Object Oriented Programing. I am going to assume you know how to set up a mysql database and the other stuff, such as securing the code for that is up to you. Though I think you’ll find out that coding in Object Oriented Style is will give you a little better security than doing it in the Procedural Style.

Let’s get started - Here is the main php file that I have in a folder called includes:
user.comments.php

<?php [php]class DatabaseConnection { private $db; private $thread_box; // Set-up array to fetch associative array from database private $user_comment = array( "id" => "", "title" => "", "content" => "" ); public $output; protected function fetch_database_comments() // protected - scope when you want to make your variable/function // visible in all classes that extend current class including the parent class. { $this->db = new mysqli("localhost", "root", "******", "your_database"); // Do Not Use in Production if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } $query = "SELECT title, content FROM comments ORDER by id DESC LIMIT 5"; // The Limit is set to 5 comments that can be displayed; however // this is obviously changeable. if ($result = $this->db->query($query)) { /* fetch associative array */ /* The following also sets it up in a nicely formatted HTML */ while ($row = $result->fetch_assoc()) { $this->thread_box = "
"; $this->thread_box .= ""; $this->thread_box .= "" . nl2br($row['title']) . ""; $this->thread_box .= "

" . nl2br($row['content']) . "

"; $this->thread_box .= ""; $this->thread_box .= "
"; $this->thread_box .= "
"; $this->output .= $this->thread_box; } /* free result set */ $result->free(); } /* close connection */ $this->db->close(); return $this->output; } } class CommentBox extends DatabaseConnection { public $output_comments; public function display_comments() { // This grabs the formatted output data from the DatabaseConnection Class in order to output to the screen. $this->output_comments = $this->fetch_database_comments(); // This displays it...pretty simple right? echo $this->output_comments; } }[/php] here's a very simple main file I call user_comments.php [php]<?php require("includes/user.comments.php"); ?> Comments <?php $comments = new CommentBox; $comments->display_comments(); //Calls method to display formatted HTML data from database. ?> [/php]

I will ever throw in the css styling file (It’s very basic just like the rest of it) :smiley:
user_style.css

[code]@charset “utf-8”;
/* CSS Document */

#user_box_style {
clear: left;
width: 490px;
border-radius: 15px;
background-image: url(…/images/img-text-bg-01.png);
background-repeat: repeat;
padding: 5px;
margin: 10px 0px 0px 0px;
}

.user_fieldset {
width: 460px;

border: 3px solid #00f;
border-radius: 15px;

}

.user_legend, .user_text_color {
color: #00f;
}[/code]

Like I said it’s a very simple programming script and I am sure it could be enhanced greatly for OOP in PHP gives you that flexibility. Like I said I also think using the Objected Oriented Style gives you greater security over the Procedural Style. Also take note that I am using mysqli and not mysql for that is deprecated - I think if you want to learn a programming right do it right the first time around. That’s my opinion and I’m sticking to it. ;D Anyways I hope this helps anyone who wants to learn PHP and Objected Oriented Programming. One last thing this code probably can be written tighter and any revisions that I do (If I think to do it) - I will post here as a reply.

Best Regards,

John

Sponsor our Newsletter | Privacy Policy | Terms of Service