Jump to content

Problems using method to re-set variable values for pagination


cgm225

Recommended Posts

I have a class posted below that I use to output notes/blog entries.  I am trying to add pagination to the output, and so in my class I need the following variables (1) page number, (2) number of notes to display, both of which I use to (3) calculate the "range" of notes/entries to display on any particular page (i.e. the query limits), and, finally, (4) the total number of entries/notes so I can calculate the total number of pages.

 

My problem is that I can calculate the total number of pages correctly using the findTotalPages() method (which sets the "range"/limits of the query very wide), but then when I try to re-set the limits of the query so I can return only the entries for any particular page, the setPageLimits() does not do so, so I end up with all the entries being displayed, not just three (if you take my example below).

 

Any ideas why the setPageLimits() is not working after using the findTotalPages() method?

 

My notes class:

<?php

class Notes {
    
    //Declaring variables
    private $connection;
    private $id;
    private $data = array();
    
    //Sets MySQLi object
    public function __construct(mysqli $connection) {
        $this->connection = $connection;
    }
    
    public function setPageNumber($pageNumber) {
        $this->pageNumber = $pageNumber;
    }
    
    public function setNumberDisplayed($numberDisplayed) {
        $this->numberDisplayed = $numberDisplayed;
    }
    
    public function setPageLimits() {
        $this->startingEntry = ($this->pageNumber - 1) * $this->numberDisplayed;
        $this->endingEntry   = $this->numberDisplayed;

        $this->setLimits($this->startingEntry, $this->endingEntry);
    }
    
    /* 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;
    }
    
    public function findTotalPages() {
        $this->setLimits();
        $this->findEntries();
        $this->totalPages = ceil(count($this->getData()) / $this->numberDisplayed);
        return $this->totalPages;
    }

    /* 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('ii', $this->skip, $this->numberRows);
        $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();
    }

    /* 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[$id] = array(
                'id'         => $id,
                'title'      => $title,
                'date'       => $date,
                'note'       => $note,
                'timestamp'  => $timestamp
            );
        }
        $statement->close();
    }
    
    //Returns the one or two dimensional array of data
    public function getData() {
        if (!empty($this->data)){
            return $this->data;
        } else {
            return false;
        }
    }
}

?>

 

My instantiation of the notes class:

        //Sets page number, number to display, and limits for page
        $notes->setPageNumber(1);
        $notes->setNumberDisplayed(3); 
        $notes->findTotalPages();  //This works...
        $notes->setPageLimits();   //This does NOT work.. the limits stay 0 and 10000 
        
        //Retrieves entry/entries array
        if ($entry) {
            //Outputs only one entry
            $notes->findEntry($entry);
            $note = $notes->getData();
        } else {
            //Outputs entries within the page limits***
            $notes->findEntries(); //THIS IS OUTPUTTING ALL ENTRIES WHICH IS NOT WHAT I WANT            
        }
?>

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.