The following just shows how to create a basic interface that Creates, Updates, Reads and Deletes. This is obviously done in Object-Oriented Programming style and is still a work-in-progress for me. However, there are benefits in creating an interface for one it is extremely portable, easy to use and easy to maintain. Two it keeps your code structured and forces you to stay structured.
Here is the interface:
<?php
/* The iCrud interface.
* The interface identifies four methods:
* - create()
* - read()
* - update()
* - delete()
*/
interface iCrud {
public function create($data);
public function read();
public function update($data);
public function delete($id=NULL);
}
[/php]
I think that is pretty straight forward and really hardly any explanation is needed.
This is how I implement the interface for my blog/forum(s).
[php]<?php
class OnlineJournal implements iCrud {
protected $query = NULL;
protected $stmt = NULL;
protected $result = NULL;
protected $query_params = NULL;
protected $row = NULL;
protected $status_message = NULL;
protected $category = 'home_page';
public function __construct(array $data = NULL) {
if (is_array($data) && !empty($data['title']) && !empty($data['message_post'])) {
$db = Database::getInstance();
$pdo = $db->getConnection();
/* Set the query variable */
$this->query = 'INSERT INTO blog (category, client_id, created_by, title, message_post, date_added) VALUES (:category, :client_id, :created_by, :title, :message_post, NOW())';
/* Set the query variable */
$this->stmt = $pdo->prepare($this->query);
/* Execute the Query */
$this->result = $this->stmt->execute(array(':category' => $this->category, ':client_id' => $data['client_id'], ':created_by' => $data['created_by'], ':title' => $data['title'], ':message_post' => $data['message_post']));
return 'Data Successfully Inserted!';
}
}
public function create($data) {
return self::__construct($data);
}
public function setCategory($category) {
$this->category = $category;
}
public function getCategory() {
return $this->category;
}
public function read() {
$db = Database::getInstance();
$pdo = $db->getConnection();
$this->query = 'SELECT id, client_id, created_by, title, message_post, date_updated, DATE_FORMAT(date_added, "%M %e, %Y") as date_added FROM blog WHERE category=:category ORDER BY date_updated ASC';
$this->stmt = $pdo->prepare($this->query);
$this->stmt->execute([':category' => $this->category]);
if ($this->stmt && $this->stmt->rowCount() > 0) {
return $this->stmt; // if there is data in the database table then return:
} else {
return FALSE;
}
}
public function update($data) {
$db = Database::getInstance();
$pdo = $db->getConnection();
/* Update the edited blog */
$this->query = 'UPDATE blog SET title=:title, message_post=:message_post, date_updated=NOW() WHERE id=:id';
/* Prepare the statement */
$this->stmt = $pdo->prepare($this->query);
/* Execute the statement */
$this->result = $this->stmt->execute(array(':title' => $data['title'], ':message_post' => $data['message_post'], ':id' => $data['id']));
return ($this->result) ? true : false;
}
public function delete($id = NULL) {
$db = Database::getInstance();
$pdo = $db->getConnection();
$this->stmt = $pdo->prepare('DELETE FROM blog WHERE id=:id');
$this->result = $this->stmt->execute(array(':id' => $id));
return ($this->result) ? true : false;
}
}
This is how new data is inserted into the database table:
<?php
require 'lib/includes/utilities.inc.php';
if (!$user) {
header("Location: forums.php");
exit();
} elseif ($user->getSecurityLevel() == 'public') {
header("Location: forums.php");
exit();
}
$category = $_SESSION['category']; // Retreive the category in order to set:
$newThread = new OnlineJournal(); // Create a new instance of C.R.U.D. system:
$newThread->setCategory($category); // Set Category for proper forum:
/* Insert blog into database table */
$journal = filter_input(INPUT_POST, 'submitBlog', FILTER_SANITIZE_SPECIAL_CHARS);
if (isset($journal) && $journal == 'Enter') {
$data['client_id'] = filter_input(INPUT_POST, 'client_id', FILTER_SANITIZE_NUMBER_INT);
$data['created_by'] = filter_input(INPUT_POST, 'created_by', FILTER_SANITIZE_SPECIAL_CHARS);
$data['title'] = filter_input(INPUT_POST, 'title', FILTER_DEFAULT);
$data['message_post'] = filter_input(INPUT_POST, 'message_post', FILTER_DEFAULT);
$message = $newThread->create($data); // insert data into database table:
unset($journal);
}
include 'lib/includes/header.inc.php';
?>
<div class="container blogContainer">
<div class="blogFormContainer">
<form id="" class="blogFormStyle m-span5" action="<?php echo $phpSelf ?>" method="post">
<fieldset class="fieldsetStyle">
<legend class="legendStyle">Insert Blog</legend>
<input type="hidden" name="update_id" value="">
<input type="hidden" name="client_id" value="<?php echo $user->getUserId(); ?>">
<input type="hidden" name="created_by" value="<?php echo $user->getFirstName() . ' ' . $user->getLastName(); ?>">
<input id="title" type="text" name="title" placeholder="Title Goes Here..." value="">
<textarea id="message_post" placeholder="Blog Text Goes Here..." name="message_post"></textarea>
<input class="blogBtn" type="submit" name="submitBlog" value="Enter">
<button class="blogBtn" name="reset" >Cancel</button>
</fieldset>
</form>
</div>
</div>
<?php
include 'lib/includes/footer.inc.php';
I think it’s pretty straight forward and like I said it’s still a work-in-progress meaning there’s still some data validation that needs to be performed on the scripts. However, I think if you know OOP then that should be a walk-in-the-park for you.