Jump to content

Recommended Posts

I just completed a notes/blog class for my website and everything works fine; however I still wanted to get some feedback.  Are there any things I could be doing better?

 

<?php

class Notes {
    
    //Declaring variables
    private $connection;
    private $id;
    private $data = array();
    
    //Sets MySQLi object
    public function __construct(mysqli $connection) {
        $this->connection = $connection;
    }
    
    /* Creates a two dimensional array in which entry id numbers are stored in
     * the first dimension, and then for each id number, a second array (i.e.
     * the second dimension) is assigned, which contains all the field values
     * for that particular entry.
     */
    public function findEntries() {
        $query = 'SELECT id, title, note, date, timestamp FROM notes ORDER BY id DESC';
        $statement = $this->connection->prepare($query);
        $statement->bind_result($id, $title, $note, $date, $timestamp);
        $statement->execute();
        while($statement->fetch()) {
            $this->data[$id] = array(
                'id'         => $id,
                'title'      => $title,
                'date'       => $date,
                'note'       => $note,
                'timestamp'  => $timestamp
            );
        }
        $statement->close();
    }
    
    //Returns the data array
    public function getData() {
        if (!empty($this->data)){
            return $this->data;
        } else {
            return false;
        }
    }
    
    //Returns the number of items in the data array
    public function getDataCount() {
        if (!empty($this->data)){
            return count($this->data);
        } else {
            return false;
        }
    }
    
    //Returns a particular range of the data in the data array
    public function getSlicedData($offset = 0, $length = 10000) {
        if (!empty($this->data)){
            return array_slice(array_values($this->data), $offset, $length);
        } else {
            return false;
        }
    }
}

?>

Link to comment
https://forums.phpfreaks.com/topic/114454-feedback-on-notesblog-class/
Share on other sites

×
×
  • 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.