A
Anonymous
Guest
I'm in the process of writing a simple cms database for my website, I actually have older cms version running my website right now. However, I'm always improving it and OOP is finally slowly sinking into my thick brain.
I just thought I would share a little test php file that I did, I created this for when I do my larger project it will be easier.
Anyways here it is, there is no css and the common.php is purposely missing. I figure if you can't figure out that, then you probably won't be able to figure out the following -
I called this object.oriented.programming.php (Call this whatever you want)
Second file: Connect.DB.Class.php
Third File: TopicFileClass.php
As you can see there isn't much to it, for it's pretty basic and OOP does tend to lead one to write cleaner code. 8)
The following is the structure for the database - if anyone cares
Table structure for table pages
Column Type Null Default
id int(11) No
blog_name varchar(60) No
content text No
new_blog_date datetime No 0000-00-00 00:00:00
update_date datetime No 0000-00-00 00:00:00
username varchar(40) No
thread int(11) No
I just thought I would share a little test php file that I did, I created this for when I do my larger project it will be easier.
Anyways here it is, there is no css and the common.php is purposely missing. I figure if you can't figure out that, then you probably won't be able to figure out the following -
I called this object.oriented.programming.php (Call this whatever you want)
Code:
<?php require('includes/Connect.DB.Class.php'); ?>
<?php require('includes/TopicFileClass.php'); ?>
<?php
$data = array();
// New instance/modifier of class MemberTopic
$topic = new MemberTopic();
// Retrieve topics from mysqli database
$topic = $topic->retrieve_record();
//print_r($topic);
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Object Oriented Programming</title>
</head>
<?php
?>
<body>
<?php foreach ($topic as $key=>$record) { // Loop through the Array to pull out the objects(records)?>
<h1><?php echo $record->blog_name; // Display the Title of the Blog ?></h1>
<p><?php echo $record->content; // Display the Content of the Blog ?></p>
<?php } // Closes the foreach loop ?>
</body>
</html>
Second file: Connect.DB.Class.php
Code:
<?php
require_once ('common.php');
abstract class ConnectMySQLClass {
protected static function connect() {
$database = new mysqli(DB_HOST,DB_USERNAME,DB_PASSWORD, DB_NAME);
return $database;
}
}
Third File: TopicFileClass.php
Code:
<?php
class MemberTopic extends ConnectMySQLClass
{
protected $threads = array();
protected function main_record()
{
$database = parent::connect(); //Connects to the mysqli Database
$query = "SELECT * FROM pages ORDER by id";
$result = $database->query($query);
while ($page = $result->fetch_array(MYSQLI_ASSOC)) {
$this->threads[] = (object) $page;
}
/* free result set */
$result->free();
return $this->threads;
}
public function retrieve_record()
{
return $this->main_record();
}
}
As you can see there isn't much to it, for it's pretty basic and OOP does tend to lead one to write cleaner code. 8)
The following is the structure for the database - if anyone cares
Table structure for table pages
Column Type Null Default
id int(11) No
blog_name varchar(60) No
content text No
new_blog_date datetime No 0000-00-00 00:00:00
update_date datetime No 0000-00-00 00:00:00
username varchar(40) No
thread int(11) No