@ bradgafelman
Line 53 is
$this->total_num_results = mysql_result ($qry, 0, "num");
Here is the whole code
<?php
class Pagination {
// Configurable parameters:
// The number of results to show on each page
private $results_per_page = 10;
// Number of page links to show each side of the current page. For example,
// if we are on page 15 and the page_range is 5 only show links to pages 10 -
// 20.
private $page_range = 5;
// The maximum number of pages to provide links for. To show all pages set
// this to -1.
private $max_pages = 50;
// Show links to first and last pages
private $show_first_and_last = false;
// Non-configurable parameters
private $from;
private $total_num_results;
private $total_pages;
private $current_page;
private $searchdata;
/**
Get the number of results and the number of pages
@ $tbl Table to query
@ $col Column in $tbl to search
@ $data Data to search for
/
function __construct($tbl, $col, $searchdata) {
$this->searchdata = mysql_real_escape_string($searchdata);
$this->from = "FROM $tbl WHERE MATCH (info) AGAINST ('$this->searchdata' IN BOOLEAN MODE)";
$qry = mysql_query("SELECT COUNT(*) AS num $this->from") or die(mysql_error());
$this->total_num_results = mysql_result ($qry, 0, "num");
$this->total_pages = ceil($this->total_num_results/$this->results_per_page);
if ($this->max_pages != -1 && $this->total_pages > $this->max_pages)
$this->total_pages = $this->max_pages;
$this->update_current_page();
}
/**
Number of results from data given to constructor
/
public function num_results() {
return $this->total_num_results;
}
/**
Number of pages from data given to constructor. This will be limited to
$this->max_pages unless $this->max_pages is -1.
*/
public function num_pages() {
return $this->total_pages;
}
/**
Execute a query to get the results. The result should be stored in a
variable. For example $qry = $p->current_page_query();. $qry can the then
be used as normal.
/
public function current_page_query() {
$start = ($this->current_page - 1) $this->results_per_page;
return mysql_query("SELECT $this->from LIMIT $start, $this->results_per_page");
}
/**
Output a basic set of pagination links e.g. <<Prev 1 2 3 Next>>
/
public function links() {
// No point outputting links if there aren't any other pages
if ($this->total_pages < 2) {
return;
}
$link_fmt = '<a href="'.$_SERVER['PHP_SELF'].'?searchdata='.$this->searchdata.'&page=%d">%s</a> ';
echo '<div class="pagination-links">';
if ($this->current_page > ($this->page_range + 1)
&& $this->show_first_and_last)
printf($link_fmt, 1, "First");
if ($this->current_page > 1)
printf($link_fmt, ($this->current_page - 1), "« Prev");
// Work out which page links to show. Always shows ($this->page_range * 2)
// pages if possible.
if ($this->current_page <= $this->page_range) {
$start = 1;
$end = $this->page_range * 2;
} else if ($this->current_page > ($this->total_pages - $this->page_range)) {
$start = $this->total_pages - ($this->page_range * 2);
$end = $this->total_pages;
} else {
$start = $this->current_page - $this->page_range;
$end = $this->current_page + $this->page_range;
}
// Make sure we don't output links to pages which don't exist
if ($end > $this->total_pages) $end = $this->total_pages;
if ($start < 1) $start = 1;
// Print the links to other pages
for ($i = $start; $i <= $end; $i++) {
if ($this->current_page == $i) {
echo $i . " ";
} else {
printf($link_fmt, $i, $i);
}
}
if ($this->current_page < $this->total_pages)
printf($link_fmt, $this->current_page + 1, "Next »");
if ($this->current_page < ($this->total_pages - $this->page_range)
&& $this->show_first_and_last)
printf($link_fmt, $this->total_pages, "Last");
echo '</div>';
}
/**
Puts the current page from the URL into $this->current_page. Checks that
$GET['page'] is within the range of pages. Will go to first or last page
as appropriate if not.
/
private function update_current_page() {
if (isset($GET['page'])) {
$this->current_page = $_GET['page'];
if ($this->current_page > $this->total_pages)
$this->current_page = $this->total_pages;
else if ($this->current_page < 1)
$this->current_page = 1;
} else {
$this->current_page = 1;
}
}
}