0
這裏是我的分頁類。構造中的數據只是另一個可以執行db查詢和其他事情的類。我遇到的問題是,我似乎無法得到數據輸出匹配分頁打印,我似乎無法得到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>
哦,但我還會怎麼做呢? $ this-> qData = $ this-> query。 「LIMIT $ start,$ limit」; $ this-> printData = $ this-> db-> query($ this-> qData);我希望能夠在while循環中寫入$ pagination-> getData之類的東西,這樣我就可以一次編寫查詢,並且類負責分頁和顯示數據。 – michael 2010-06-02 00:28:01
在我看來,while循環需要在show()函數內。 – jeroen 2010-06-02 01:11:03