Board index   FAQ   Search  
Register  Login
Board index PHP Your Scripts

A simple OOP on arrays and objects

This is where you share YOUR scripts with others

Moderators: macek, egami, gesf

A simple OOP on arrays and objects

Postby Strider64 » Fri Apr 12, 2013 8:34 pm

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. :D

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 :D -

I called this object.oriented.programming.php (Call this whatever you want)
Code: Select all
<?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: Select all
<?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: Select all
<?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
User avatar
Strider64
New php-forum User
New php-forum User
 
Posts: 44
Joined: Sat Mar 23, 2013 8:24 am

Re: A simple OOP on arrays and objects

Postby Strider64 » Tue Apr 16, 2013 9:29 pm

I just have to say I made the code a little bit better....

I would explain this code, but it's getting late. I explain it tomorrow if I remember. :D

Code: Select all
<?php
class BlogPost extends ConnectMySQLClass {
     
    public $send_comments = array();
   
    public $send_posts = array();
   
   // Get the total rows in the pages table
   public function get_rows_from_pages()
   {
       $database = parent::connect();
      
       if ($result = $database->query("SELECT * FROM pages ORDER BY id")) {
      
         /* determine number of rows result set */
         $row_cnt = $result->num_rows;
      
         $this->rows = $row_cnt;
      
         /* close result set */
         $result->close();
       }
       
       return $this->rows;
    } 

    public function blogPostRecords()
   {
      $database = parent::connect(); //Connects to the mysqli Database
      
      $query = "SELECT id, blog_name, content, new_blog_date, update_date, username FROM pages ORDER by id DESC";
      $result = $database->query($query);
      
      while ($page = $result->fetch_array(MYSQLI_ASSOC)) {
   
         $this->send_posts[] = new BlogPosts($page);                  
      }
      /* free result set */
      $result->free();
      
      
      return $this->send_posts;
      
   }
   
    // Insert a new topic into the comments table - A user is assigned to the comment.
    public function insert_new_topic($page) {
          $database = parent::connect();
       
        $stmt = $database->prepare("INSERT INTO pages (blog_name, content, new_blog_date, update_date, username) VALUES (?,?,?,?,?)");
          $stmt->bind_param('sssss',$page['blog_name'], $page['content'], $page['new_blog_date'], $page['update_date'], $page['username']);       
      
        /* execute prepared statement */
        $stmt->execute();              
       
        /* close statement and connection */
        $stmt->close();   
      
           header('Location: members.php'); // redirect to members page
           exit;             
    }
   
   
    public function commentPostRecords()
   {
      $database = parent::connect(); //Connects to the mysqli Database
      
      $query = "SELECT id, username, pages_id, content, post_date FROM comments ORDER by id";
      $result = $database->query($query);
      
      while ($page = $result->fetch_array(MYSQLI_ASSOC)) {         
         
         $this->send_comments[] = new BlogPosts($page);   // Assign new object BlogPosts to an array
      }
      /* free result set */
      $result->free();
      
      return $this->send_comments;
      
   } 
 
}


Code: Select all
<?php
class BlogPosts extends BlogPost {
   
    public function  __construct($page) {
       foreach ($page as $key => $value)
       {         
         $pos = strpos($key, 'date'); // Find the word date in array
         if ( $pos !== false ) $value = date('F j, Y g:i A', strtotime( $value )); // Format MySQL date to proper format
         $this->$key = $value; // Assign value to variable in object.
       }

    }
   
}
User avatar
Strider64
New php-forum User
New php-forum User
 
Posts: 44
Joined: Sat Mar 23, 2013 8:24 am


Return to Your Scripts

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.