Jump to content

How would you combine echoed output of class...


cgm225

Recommended Posts

I have a notes/blog class that returns entries from a MySQL database.  Everything works fine, but I wanted to know if there was a better way to implement the class.

 

Currently, I can grab either (1) many entries or (2) one entry, and, depending on which, I output the returned data (1) within a foreach loop, or (2) within a simple if statement.

 

My question is, is there a way to make my procedural code more efficient where I am only needing one block of code to echo these variables, not two?  Any other feedback is also appreciated

 

Thanks in advance!

 

 

Example use of notes/blog class:

        //Setting entry variable
        $regex  = '/[^-_A-z0-9]++/';
        $entry = isset($_GET['entry']) ? preg_replace($regex, '', $_GET['entry']) : null;

        //Setting MySQLi connection object and selecting database
        $mysqli = $this->registry->get('mysqli');
        $mysqli->select_db('exampl_db');

        //Instatiating new Notes class
        $notes = new Notes($mysqli);

        if (!$entry) {

            $notes->setLimits();
            $notes->findEntries();

            //First block of procedural code outputting many entries
            foreach($notes->getData() as $note) {
                echo $note['id'] . "<br>";
                echo $note['title'] . "<br>";
                echo $note['date'] . "<br>";
                echo nl2br($note['note']) . "<br>";
                echo $note['timestamp'] . "<br>";
            }

        } else {
    
            $notes->findEntry($entry);
            $note = $notes->getData();
            
            //Second block of procedural code outputting one entry
            if ($note) {
                echo $note['id'] . "<br>";
                echo $note['title'] . "<br>";
                echo $note['date'] . "<br>";
                echo nl2br($note['note']) . "<br>";
                echo $note['timestamp'] . "<br>";
            }
        }

 

Notes class:

<?php

class Notes {
    
    //Declaring variables
    private $connection;
    private $id;
    private $data = array();
    
    //Sets MySQLi connection
    public function __construct(mysqli $connection) {
        $this->connection = $connection;
    }
    
    /* Sets limits for prepared statement used in the getEntries() method.  If
     * limits are not manually set, then default values are used.
     */
    public function setLimits($skip = 0, $numberRows = 10000) {
        $this->skip = $skip;
        $this->numberRows = $numberRows;
    }

    /* 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 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(
                'id'         => $id,
                'title'      => $title,
                'date'       => $date,
                'note'       => $note,
                'timestamp'  => $timestamp
            );
        }
    }

    /* Creates a one dimensional array in which an entry's id number and all
     * other field values are stored.
     */
    public function findEntry($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
            );
        }
    }
    
    //Returns the one or two dimensional array of data
    public function getData() {
        if (!empty($this->data)){
            return $this->data;
        } else {
            return false;
        }
    }
}

?>

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.