这是我的分页课。构造中的数据只是另一个执行数据库查询和其他内容的类。我遇到的问题是,我似乎无法获得数据输出以匹配分页打印,我似乎无法获取mysql_fetch_assoc()来查询数据并将其打印出来。我收到这个错误:
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /phpdev/pagination/index.php on line 215
我知道这可能意味着查询不正确,它可以获取数据,但我已正确输入。抱歉脚本有点长。我提前感谢所有人的帮助。
<?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>
答案 0 :(得分:0)
您的变量$pagination->printData
已在show()
函数中设置,但您在while
循环后调用该函数,因此没有查询结果$pagination->printData
当您在while
循环中使用它时。{/ 1>