简体   繁体   English

分页$ _GET ['page']在php中

[英]pagination $_GET['page'] in php

I have a pagination question. 我有一个分页问题。 When I load this page in the browser and click on the category link to show my products the first page shows with the products limited and the links for pagination are in place but it keeps sending me to the first page which is zero for whatever pagination link I click on. 当我在浏览器中加载此页面并单击类别链接以显示我的产品时,第一页显示的产品受到限制,并且分页链接到位,但无论如何发送分页链接,它始终将我发送到第一页,该页面为零我点击。 But the odd thing is is when I change 但是奇怪的是当我改变

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

to

$offset=($pageNum)= $perPage;

if shows the rest of the products I'm trying to show after clicking on the category. 如果显示的是单击类别后要显示的其余产品。 So the problem might be in the page or somewhere around there. 因此问题可能出在页面上或附近。

Here is my code. 这是我的代码。

<?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>';
//
//} 
//}
?>

Thanks for all you help!!! 感谢您的帮助!!!

I'll ignore the inefficiencies (refer to comments on your question). 我会忽略效率低下的问题(请参阅对您问题的评论)。 The problem is not your offsetting code—that works fine. 问题不在于您的抵消代码,它可以正常工作。 Your links are broken. 您的链接已损坏。

When generating your numbered links into $nav , you need to append, not overwrite. 将编号链接生成到$nav ,您需要追加而不是覆盖。 Use .= , not = . 使用.=而不是= Also, beware of capitalization. 另外,请注意大写。 $maxpages is not $maxPages . $maxpages不是$maxPages

Here's updated code. 这是更新的代码。 Proof this works . 证明这行得通 Unless your database query is misconstructed (I can't test that, sorry!), you should be good to go. 除非您的数据库查询结构错误(抱歉,我无法测试),否则您应该一切顺利。

$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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM