dsgwoh7038 2016-08-14 17:43
浏览 20

PHP分页与多个GET变量

I'm struggling with pagination with 2 get variables - 1 of which being the page number.

My second $_GET variable I have is $_GET['cat'] which is getting the category of products I want to filter on.

Everything works with the pagination DISPLAY, number of pages etc., however, if I am filtered on a particular category, the first page of results displays fine, but when i try to get to the next page or any page number, nothing is displayed product wise.

Any help would be appreciated, please see code below.

<?php include("header.php");
echo '<div id="content_header"><center><h1> • Products • </h1></center></div>';
echo '<div id="products_left">
                Show:<br />
                <ul style="list-style-type:none; margin:0; padding:0;">
                    <li><a href="?cat=wedding">Wedding Stationery</a></li>
                    <li><a href="?cat=frame">Frames</a></li>
                    <li><a href="?cat=nappy">Nappy Cakes</a></li>
                    <li><a href="?cat=misc">Miscellaneous</a></li>
                    <li><a href="/michelle/products.php">All</a></li>
                </ul>
            </div>';
echo '<div id="clear"></div>';
echo '<div style="padding-left:150px; margin-top:-70px">';
$connect = mysqli_connect("localhost","root","") or die("Failed to connect to database");
mysqli_select_db($connect,"magical_moments") or die("Unable to find database");

if(empty($_GET['cat'])){
    $query = mysqli_query($connect,"SELECT * FROM products");
} else {
    $cat = $_GET['cat'];
    $query = mysqli_query($connect,"SELECT * FROM products WHERE type='$cat'");

};

$products = mysqli_fetch_array($query);

$start = 0;
$limit = 9;

if(isset($_GET['page']))
{
    $pid=$_GET['page'];
    $start=($pid-1)*$limit;
}
else{
    $pid=1;
}
//Fetch from database first 10 items which is its limit. For that when page open you can see first 10 items.
if(empty($_GET['cat'])){
    $query = mysqli_query($connect,"SELECT * FROM products LIMIT $start, $limit");
} else {
    $cat = $_GET['cat'];
    $query = mysqli_query($connect,"SELECT * FROM products WHERE type='$cat' LIMIT $start, $limit");

};

?>
<ul>
<?php
//print 10 items
while($result=mysqli_fetch_array($query))
{
        echo '<div id="display_product">
                        <table>
                        <tr>
                            <td><div id="product_image_holder" style="background-image:url('.$result[5].')"></div></td>
                        </tr>
                            <tr>
                                <td>'.$result[1].'</td>
                            </tr>
                            <tr>
                                <td>'.$result[3].'</td>
                            </tr>
                            <tr>
                                <td>£ '.$result[4].'</td>
                            </tr>
                        </table>
                    </div>';
}
echo '<div id="clear"></div>';
?>
</ul>
<?php
//fetch all the data from database.


echo '<div id="pagination_div">';

if(empty($_GET['cat'])) {

    $rows=mysqli_num_rows(mysqli_query($connect,"SELECT * FROM products"));
    //calculate total page number for the given table in the database
    $total=ceil($rows/$limit);

if($pid>1)
{
    //Go to previous page to show previous 10 items. If its in page 1 then it is inactive
    echo "<a href='?page=".($pid-1)."' class='previous_button'>PREVIOUS</a>";
}
if($pid!=$total)
{
    ////Go to previous page to show next 10 items.
    echo "<a href='?page=".($pid+1)."' class='next_button'>NEXT</a>";
}

//show all the page link with page number. When click on these numbers go to particular page.
        for($i=1;$i<=$total;$i++)
        {
            if($i==$pid) { echo "<li class='current'>".$i."</li>"; }

            else { echo "<li class='pagination'><a href='?page=".$i."'>".$i."</a></li>"; }
        }
        echo '</div>';

} else {

    $rows=mysqli_num_rows(mysqli_query($connect,"SELECT * FROM products WHERE type='$cat'"));
    //calculate total page number for the given table in the database
    $total=ceil($rows/$limit);

     // cat is set - pagination for cat + page

     if($pid>1)
     {
        //Go to previous page to show previous 10 items. If its in page 1 then it is inactive
        echo "<a href=".$_SERVER['REQUEST_URI']."?page=".($pid-1)." class='previous_button'>PREVIOUS</a>";
     }
     if($pid!=$total)
     {
        ////Go to previous page to show next 10 items.
        echo "<a href=".$_SERVER['REQUEST_URI']."?page=".($pid+1)." class='next_button'>NEXT</a>";
     }

     //show all the page link with page number. When click on these numbers go to particular page.
            for($i=1;$i<=$total;$i++)
            {
                if($i==$pid) { echo "<li class='current'>".$i."</li>"; }

                else { echo "<li class='pagination'><a href=".$_SERVER['REQUEST_URI']."?page=".$i.">".$i."</a></li>"; }
            }
            echo '</div>';

        };

?>
</ul>
</div>
<?php include("footer.php"); ?>
  • 写回答

1条回答 默认 最新

  • dongxun1244 2016-08-14 18:07
    关注

    Well, you define the page parameter. But the category parameter is lost once clicked on a link because it's simply not there.

    You've got this:

    echo "<li class='pagination'><a href=".$_SERVER['REQUEST_URI']."?page=".$i.">".$i."</a></li>";
    

    You lack the parameter cat. Here's an example:

    echo '<li class="pagination"><a href="'.$_SERVER['REQUEST_URI'].'?page='.$i.'&cat='.$_GET['cat'].'">'.$i.'</a></li>';
    
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向