Jump to content

Feedback on Notes/blog classes.. would you do anything differently?


cgm225

Recommended Posts

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();

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();
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.