help fixing pagination class
- by michael
here is my pagination class. the data in the construct is just an another class that does db queries and other stuff. the problem that i am having is that i cant seem to get the data output to match the pagination printing and i cant seem to get the mysql_fetch_assoc() to query the data and print it out. i get this error:
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /phpdev/pagination/index.php on line 215
i know that can mean that the query isnt correct for it to fetch the data but i have entered it correctly. sorry for the script being a little long. i thank all in advance for any help.
<?php
include_once("class.db.php");
mysql_connect("host","login","password");
mysql_select_db("iadcart");
class Pagination {
var $adjacents;
var $limit;
var $param;
var $mPage;
var $query;
var $qData;
var $printData;
protected $db;
function __construct() {
$this->db = new MysqlDB;
}
function show() {
$result = $this->db->query($this->query);
$totalPages = $this->db->num_rows($result);
$limit = $this->limit;
/* -------------------------------------
Set Page
------------------------------------- */
if(isset($_GET['page'])) {
$page = intval($_GET['page']);
$start = ($page - 1) * $limit;
} else {
$start = 0;
}
/* -------------------------------------
Set Page Vars
------------------------------------- */
if($page == 0) {
$page = 1;
}
$prev = $page - 1;
$next = $page + 1;
$lastPage = ceil($totalPages/$limit);
$lpm1 = $lastPage - 1;
$adjacents = $this->adjacents;
$mPage = $this->mPage;
//the magic
$this->qData = $this->query . " LIMIT $start, $limit";
$this->printData = $this->db->query($this->qData);
/* -------------------------------------
Draw Pagination Object
------------------------------------- */
$pagination = "";
if($lastPage > 1) {
$pagination .= "<div class='pagination'>";
}
/* -------------------------------------
Previous Link
------------------------------------- */
if($page > 1) {
$pagination .= "<li><a href='$mPage?page=$prev'> previous </a></li>";
} else {
$pagination .= "<li class='previous-off'> previous </li>";
}
/* -------------------------------------
Page Numbers (not enough pages - just display active page)
------------------------------------- */
if($lastPage < 7 + ($adjacents * 2)) {
for($counter = 1; $counter <= $lastPage; $counter++) {
if($counter == $page) {
$pagination .= "<li class='active'>$counter</li>";
} else {
$pagination .= "<li><a href='$mPage?page=$counter'>$counter</a></li>";
}
}
}
/* -------------------------------------
Page Numbers (enough pages to paginate)
------------------------------------- */
elseif($lastPage > 5 + ($adjacents * 2)) {
//close to beginning; only hide later pages
if($page < 1 + ($adjacents * 2)) {
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++) {
if ($counter == $page) {
$pagination.= "<li class='active'>$counter</li>";
}
else {
$pagination.= "<li><a href='$mPage?page=$counter'>$counter</a></li>";
}
}
$pagination.= "...";
$pagination.= "<li><a href='$mPage?page=$lpm1'>$lpm1</a></li>";
$pagination.= "<li><a href='$mPage?page=$lastPage'>$lastPage</a></li>";
}
elseif($lastPage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) {
$pagination.= "<li><a href='$mPage?page=1'>1</a></li>";
$pagination.= "<li><a href='$mPage?page=2'>2</a></li>";
$pagination.= "...";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++) {
if ($counter == $page) {
$pagination.= "<li class='active'>$counter</li>";
}
else {
$pagination.= "<li><a href='$mPage?page=$counter'>$counter</a></li>";
}
}
$pagination.= "...";
$pagination.= "<li><a href='$mPage?page=$lpm1'>$lpm1</a></li>";
$pagination.= "<li><a href='$mPage?page=$lastPage'>$lastPage</a></li>";
}
else {
$pagination.= "<li><a href='$mPage?page=1'>1</a></li>";
$pagination.= "</li><a href='$mPage?page=2'>2</a></li>";
$pagination.= "...";
for ($counter = $lastPage - (2 + ($adjacents * 2)); $counter <= $lastPage; $counter++) {
if ($counter == $page) {
$pagination.= "<li class='active'>$counter</li>";
}
else {
$pagination.= "<li><a href='$mPage?page=$counter'>$counter</a></li>";
}
}
}
}
/* -------------------------------------
Next Link
------------------------------------- */
if ($page < $counter - 1) {
$pagination.= "<li><a href='$mPage?page=$next'> Next </a></li>";
}
else {
$pagination.= "<li class='next-off'> Next </li>";
$pagination.= "</div>";
}
print $pagination;
}
}
?>
<html>
<head>
</head>
<body>
<table style="width:800px;">
<?php
mysql_connect("localhost","root","games818");
mysql_select_db("iadcart");
$pagination = new pagination;
$pagination->adjacents = 3;
$pagination->limit = 10;
$pagination->param = $_GET['page'];
$pagination->mPage = "index.php";
$pagination->query = "select * from tbl_products where pVisible = 'yes'";
while($row = mysql_fetch_assoc($pagination->printData)) {
print $row['pName'];
}
?>
</table>
<br/><br/>
<?php $pagination->show(); ?>
</body>
</html>