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:
Code: Select all
<?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;
}
}
}
?>Code: Select all
<?php
//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
}
?>