2012-05-01 57 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