cgm225 Posted May 15, 2008 Share Posted May 15, 2008 I wrote a simple set of classes to display either (1) a single note/blog entry or (2) all note/blog entries, which I have included below. I wanted to get some feedback on them. Is there anything you would do differently? $mysqli = new mysqli(MYSQL_SERVER,MYSQL_SERVER_USERNAME,MYSQL_SERVER_PASSWORD); abstract class Notes { protected $connection; protected $data = array(); protected function __construct(mysqli $connection) { $this->connection = $connection; } public function getData() { return $this->data; } } class OneNote extends Notes { private $entry; public function __construct(mysqli $connection, $id) { parent::__construct($connection); $this->id = $id; $this->getEntry(); } private function getEntry() { $query = "SELECT id, title, date, note, timestamp FROM notes WHERE id = ?"; $statement = $this->connection->prepare($query); $statement->bind_param('s', $this->id); $statement->bind_result($id, $title, $date, $note, $timestamp); $statement->execute(); if($statement->fetch()) { $this->data = array( 'id' => $id, 'title' => $title, 'date' => $date, 'note' => $note, 'timestamp' => $timestamp ); } } } class AllNotes extends Notes { public function __construct(mysqli $connection) { parent::__construct($connection); $this->getEntry(); } private function getEntry() { $query = "SELECT id, title, note, date, timestamp FROM notes ORDER BY id DESC"; $statement = $this->connection->prepare($query); $statement->bind_result($id, $title, $date, $note, $timestamp); $statement->execute(); while($statement->fetch()) { $this->data[$id] = array( //Creating two dimensional array 'id' => $id, 'title' => $title, 'date' => $date, 'note' => $note, 'timestamp' => $timestamp ); } } } $mysqli->select_db('example_db'); $notes = new AllNotes($mysqli); $data = $notes->getData(); foreach($data as $note) { echo htmlentities($note['id']) . "<br>"; echo htmlentities($note['title']) . "<br>"; echo htmlentities($note['date']) . "<br>"; echo stripslashes($note['note']) . "<br>"; echo htmlentities($note['timestamp']) . "<br>"; } $note = new OneNote($mysqli, 'example_id'); $data = $note->getData(); echo htmlentities($data['id']) . "<br>"; echo htmlentities($data['title']) . "<br>"; echo htmlentities($data['date']) . "<br>"; echo stripslashes($data['note']) . "<br>"; echo htmlentities($data['timestamp']) . "<br>"; $mysqli->close(); Link to comment https://forums.phpfreaks.com/topic/105727-feedback-on-notesblog-classes-would-you-do-anything-differently/ Share on other sites More sharing options...
blueman378 Posted May 15, 2008 Share Posted May 15, 2008 sorry bro jsut closed it in php tags so it gets color coded otherwise i cant read it <?php $mysqli = new mysqli(MYSQL_SERVER,MYSQL_SERVER_USERNAME,MYSQL_SERVER_PASSWORD); abstract class Notes { protected $connection; protected $data = array(); protected function __construct(mysqli $connection) { $this->connection = $connection; } public function getData() { return $this->data; } } class OneNote extends Notes { private $entry; public function __construct(mysqli $connection, $id) { parent::__construct($connection); $this->id = $id; $this->getEntry(); } private function getEntry() { $query = "SELECT id, title, date, note, timestamp FROM notes WHERE id = ?"; $statement = $this->connection->prepare($query); $statement->bind_param('s', $this->id); $statement->bind_result($id, $title, $date, $note, $timestamp); $statement->execute(); if($statement->fetch()) { $this->data = array( 'id' => $id, 'title' => $title, 'date' => $date, 'note' => $note, 'timestamp' => $timestamp ); } } } class AllNotes extends Notes { public function __construct(mysqli $connection) { parent::__construct($connection); $this->getEntry(); } private function getEntry() { $query = "SELECT id, title, note, date, timestamp FROM notes ORDER BY id DESC"; $statement = $this->connection->prepare($query); $statement->bind_result($id, $title, $date, $note, $timestamp); $statement->execute(); while($statement->fetch()) { $this->data[$id] = array( //Creating two dimensional array 'id' => $id, 'title' => $title, 'date' => $date, 'note' => $note, 'timestamp' => $timestamp ); } } } $mysqli->select_db('example_db'); $notes = new AllNotes($mysqli); $data = $notes->getData(); foreach($data as $note) { echo htmlentities($note['id']) . "<br>"; echo htmlentities($note['title']) . "<br>"; echo htmlentities($note['date']) . "<br>"; echo stripslashes($note['note']) . "<br>"; echo htmlentities($note['timestamp']) . "<br>"; } $note = new OneNote($mysqli, 'example_id'); $data = $note->getData(); echo htmlentities($data['id']) . "<br>"; echo htmlentities($data['title']) . "<br>"; echo htmlentities($data['date']) . "<br>"; echo stripslashes($data['note']) . "<br>"; echo htmlentities($data['timestamp']) . "<br>"; $mysqli->close(); ?> Link to comment https://forums.phpfreaks.com/topic/105727-feedback-on-notesblog-classes-would-you-do-anything-differently/#findComment-541745 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.