2012-05-01 60 views
0

我有一個分頁問題。當我在瀏覽器中加載此頁面並單擊類別鏈接以顯示我的產品時,第一頁顯示的是產品受限且分頁鏈接已到位,但它始終將我發送到第一頁,該頁面對於任何分頁鏈接爲零我點擊。但是,當我改變

$offset=($pageNum - 1) * $perPage; 

$offset=($pageNum)= $perPage; 

如果顯示我試圖點擊類別後,以顯示產品的其他奇怪的是是。所以問題可能出現在頁面或其他地方。

這是我的代碼。

<?php 
$productUlList=""; 
error_reporting (E_ALL^E_NOTICE); 
include_once "convenientglobal2localhost.php"; 
$result = mysql_query("SELECT * FROM category WHERE 1")or die(mysql_error()); 


while($rowp=mysql_fetch_array($result)){ 
    $categoryId=$rowp['catId']; 
    $categoryName=$rowp['catName']; 
$productUlList.=' 
    <ul id="ul" > 
     <li id="lists"> <a href="'.$_SERVER['PHP_SELF'].'?category='.$categoryName.'"> '.$categoryName.' </a> </li> 
    </ul>'; 
} 
?> 
<?php 
$msg_to_user3=''; 

$productList.=''; 
$categoryList=''; 
include_once "convenientglobal2localhost.php"; 
$perPage= 3; 
if(isset($_GET['category'])) 


$categoryNames=$_GET['category']; 
$pageNum=(isset($_GET['page']))? (int)$_GET['page']: 1; 
$pages_query= mysql_query("SELECT * FROM products INNER JOIN category ON categoryName=catName WHERE categoryName='$categoryNames'"); 
$numrows= mysql_num_rows($pages_query); 
$maxpages=ceil($numrows/$perPage); 
$offset=($pageNum-1) * $perPage; 
if ($offset < 0) 
{ 
$offset = 0 ; 
} 


include_once "convenientglobal2localhost.php"; 
$results = mysql_query("SELECT * FROM products WHERE categoryName='$categoryNames' LIMIT $offset, $perPage")or die(mysql_error()); 
$num=mysql_num_rows($results); 

if($num > 0){ 


while($row=mysql_fetch_array($results)){ 


$productId=$row['productId']; 
$productName=$row['name']; 
$productDescription=$row['description']; 
$productPrice=$row['price']; 
$productDiscountedPrice=$row['discountedPrice']; 
$productStock=$row['stock']; 
$productCategory=$row['categoryName']; 
$categoryId=$row['catId']; 
$catName=$row['catName']; 

$categoryList='<table><th id="toptable"></th></table> 
<table id="categorytable"> 
<th><img src="inventory_category_images/' . $categoryId . '.jpg" width="498px"; height="125px";/></th> 
</table>'; 
$productList.='<table id="productoutputtable"> 
<tr> 
<td rowspan="7" valign="top"><img style="border-style=solid; border-color:#767475; padding=; "src="inventory_images/' . $productId . '.jpg" width="150" height="135"/> 
    </td> 
    </tr> 
    <tr> 
    <td id="tablecolor" ><strong>Product</strong></td> 
    <td colspan="2">' . $productName . ' </td> 
    <td id="tablecolor"><strong>Category</strong></td> 
    <td>' . $productCategory . ' </td> 
    </tr> 
    <tr> 
    <td id="tablecolor"><strong>Description:</strong></td> 
    <td colspan="3">' . $productDescription . ' </td> 
    </tr> 
    <tr> 
    <td id="tablecolor" ><strong>Price:</strong></td> 
    <td>$' . $productPrice . ' </td> 
    </tr><tr> 
    <td id="tablecolor"colspan="1"><strong>Sale Price:</strong></td> 
    <td>$' . $productDiscountedPrice . ' </td> 
    <td id="tablecolor"colspan="2"><strong>In Stock </strong></td> 
    <td>' . $productStock . ' </td> 
    </tr> 
    </table>'; 

} 

$self= $_SERVER['PHP_SELF']; 
for($page=1; $page<=$maxpages; $page++){ 
    if($page == $pageNum){ 
     $nav= "$page"; 
    } 
    else{ 
     $nav= "<a href=\"$self?page=$page\">$page</a>"; 
    } 
} 

if($page > 1) 
{ 
    $page=$pageNum-1; 
    $prev ="<a href=\"$self?page=$page\">[Prev]</a>"; 
    $first="<a href=\"$self?page=1\">[First Page]</a>"; 
} 
else 
{ 
    $prev= "&nbsp"; 
    $first="&nbsp"; 
} 
if($pageNum < $maxPages) 
{ 
    $page=$pageNum+1; 
    $next ="<a href=\"$self?page=$page\">[Next]</a>"; 
    $last="<a href=\"$self?page=$maxPages\">[Last Page]</a>"; 
} 
else 
{ 
    $next= "&nbsp"; 
    $last="&nbsp"; 
} 
$pageList.= $first. $prev. $nav. $next. $last; 

} 
else{ 
    $msg_to_user3="You have no products listed."; 

} 
//$pageList.=""; 
//for($i = 0 ; $i <= $maxpages ; $i++) { 
//if($i == $page) { 
//$pageList.= "<B>$i</B>"; 
//}else{ 
//$pageList.= '<A HREF="?page='.$i.'">'.$i.'</A>'; 
// 
//} 
//} 
?> 

感謝您的幫助!

+0

究竟是什麼問題?跟蹤這麼長的代碼很困難。 – Lion

+1

不要做'select *'然後使用mysql_num_rows()。你迫使數據庫浪費大量精力從剛剛扔掉的磁盤中獲取數據。使用'select count(*)'並從中取出一行結果。 –

+0

僅供參考,如果您尚未註冊,則您已**全面開放** SQL注入,並且您**將被黑客入侵。學習如何使用PDO準備的查詢完全避免此問題。 – Brad

回答

1

我會忽略低效率(請參閱您的問題的評論)。問題不在於你的抵消代碼 - 工作正常。您的鏈接已損壞。

生成編號爲$nav的鏈接時,需要追加而不是覆蓋。使用.=,而不是=。另外,要小心大寫。 $maxpages不是$maxPages

這是更新的代碼。 Proof this works。除非你的數據庫查詢是錯誤的(我無法測試這個,對不起!),你應該很好走。

$self= $_SERVER['PHP_SELF']; 
for($page=1; $page<=$maxpages; $page++){ 
    if($page == $pageNum){ 
     $nav.= "$page"; 
    } 
    else{ 
     $nav.= "<a href=\"$self?page=$page\">$page</a>"; 
    } 
} 

if($page > 1) 
{ 
    $page=$pageNum-1; 
    $prev ="<a href=\"$self?page=$page\">[Prev]</a>"; 
    $first="<a href=\"$self?page=1\">[First Page]</a>"; 
} 
else 
{ 
    $prev= "&nbsp"; 
    $first="&nbsp"; 
} 
if($pageNum < $maxpages) 
{ 
    $page=$pageNum+1; 
    $next ="<a href=\"$self?page=$page\">[Next]</a>"; 
    $last="<a href=\"$self?page=$maxpages\">[Last Page]</a>"; 
} 
else 
{ 
    $next= "&nbsp"; 
    $last="&nbsp"; 
} 
$pageList.= $first. $prev. $nav. $next. $last; 
+0

那麼,儘管顯示所有的分頁鏈接,但我仍然無法進入產品展示的第二頁。它說我沒有列出任何產品,這是我的SQL num_rows語句。我不知道還有什麼要做。 –

+0

手動在數據庫中運行'SELECT * FROM products WHERE categoryName ='$ categoryNames'LIMIT 3,3',看看你得到了什麼。你的邏輯似乎有效,所以查詢必須返回零結果。你確定你正在輸入一個有效的類別嗎?該表是否實際命名爲產品? – benesch

+0

如果可能,「手動」是指通過PHPMyAdmin或其他數據庫管理程序。 – benesch