Jump to content

Feedback on Notes/blog entry class...


cgm225

Recommended Posts

I wanted to get some feedback on a notes/blog entry class I have written to display posts stored in an MySQL database.  What can I improve?  What would you do differently?  I am VERY new to OOP.

 


class Notes {
    private $connection;
    private $id;
    
    public function __construct(mysqli $connection) {
        $this->connection = $connection;
    }
    
    public function setLimits($skip = 0, $numberRows = 10000) {
        $this->skip = $skip;
        $this->numberRows = $numberRows;
    }

    public function getEntries() {
        $query = "SELECT id, title, note, date, timestamp FROM notes ORDER BY id DESC LIMIT ?, ?";
        $statement = $this->connection->prepare($query);
        $statement->bind_param('is', $this->skip, $this->numberRows);
        $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
            );
        }
    }
    
    public function getEntry($id) {
        $query = "SELECT id, title, date, note, timestamp FROM notes WHERE id = ?";
        $statement = $this->connection->prepare($query);
        $statement->bind_param('s', $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
            );
        }
    }
    
    public function getData() {
        return $this->data;
    }
}


//////EXAMPLE OF USAGE///////

$mysqli = new mysqli(MYSQL_SERVER,MYSQL_SERVER_USERNAME,MYSQL_SERVER_PASSWORD);

$mysqli->select_db('example_db');
$notes = new Notes($mysqli);


$notes->setLimits(0,2); //example limits
$notes->getEntries();
$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>";
}

$notes->getEntry('example_id');
$data = $notes->getData();
foreach($data as $note) {
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/105923-feedback-on-notesblog-entry-class/
Share on other sites

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.