php – 分页和$_post含义的解决方案

您好我刚刚完成我的分页并面临我的过滤系统问题,我使用过滤器和$_POST提取数据,但事情是当我去第二页时,帖子意思是什么都没有,我错过了一些数据然后在我的案件我得到更多然后我过滤它,我的意思是如果我按位置过滤(伦敦)我得到36的36但当我去第二页我会得到所有36因为$_post失去了他的意思野兔是我的代码和实况页面:
My website

热门代码:

$cat1 = '';
$perpage = 10;

if(isset($_GET["catid"])){

    $p1 = '';
    $p2 = '';
    $catid = $_GET["catid"];
    $l1 = substr($catid,0,1);
    $l2 = substr($catid,1,1);
    $p1 = "CAT".$l1;
    if(!empty($l2)){
        $p2 = "CAT".$l1."-".$l2;
        $p3 = $p2;
    }
    $cat1 = @$lang[$p1];
    $cat2 = @$lang[$p2];   
}

$postid = '';
$userid = '';
$pricemin = '';
$pricemax = '';
$location = '';

if(isset($_POST["filter"])){

$pricemin = $_POST["min"];
$pricemax = $_POST["max"];
$location = $_POST["location"];
}

///////////////////////////////////////PAGINATION //////////////////

                    if(empty($p1) && empty($p2)){
                        $sql = "SELECT * FROM posts p JOIN images i ON p.id = i.postid WHERE p.id > 0 ";

                        if(!empty($location)){
                            $sql .= "AND location='$location'";
                        }
                        if(!empty($pricemin)){
                            $sql .= "AND price>='$pricemin' ";
                        }
                        if(!empty($pricemax)){

                            $sql .= "AND price<='$pricemax' ";
                        }
                    } else if(!empty($p2)){
                        $sql = "SELECT * FROM posts p JOIN images i ON p.id = i.postid WHERE catid='$p2' ";
                        if(!empty($location)){
                            $sql .= "AND location='$location'";
                        }
                        if(!empty($pricemin)){
                            $sql .= "AND price>='$pricemin' ";
                        }
                        if(!empty($pricemax)){
                            $sql .= "AND price<='$pricemax' ";
                        }
                    } else {
                        $sql = "SELECT * FROM posts p JOIN images i ON p.id = i.postid WHERE p.catid LIKE '$p1%' ";
                        if(!empty($location)){
                            $sql .= "AND location='$location'";
                        }
                        if(!empty($pricemin)){
                            $sql .= "AND price>='$pricemin' ";
                        }
                        if(!empty($pricemax)){
                            $sql .= "AND price<='$pricemax' ";
                        }
                    }
                    $res = mysqli_query($connect,$sql);
                    $rows = mysqli_num_rows($res);
                    $last = ceil($rows/$perpage);

                    if($last < 1){
                        $last = 1;
                    }

                    $pagenum = 1;

                    if(isset($_GET["pn"])){
                        $pagenum = preg_replace("#[^0-9]#", "", $_GET["pn"]);
                    }

                    if($pagenum < 1){
                        $pagenum = 1;
                    } else if($pagenum > $last){
                        $pagenum = $last; 
                    }
                    $limit = 'LIMIT ' .($pagenum - 1) * $perpage.',' .$perpage;



    $paginationCtrls = '';

if($last != 1){

    if ($pagenum > 1) {
        $previous = $pagenum - 1;
        $paginationCtrls .= '<a href="cat.php?catid='.$catid.'&pn='.$previous.'">Previous</a> &nbsp; &nbsp; ';

        for($i = $pagenum-4; $i < $pagenum; $i++){
            if($i > 0){
                $paginationCtrls .= '<a href="cat.php?catid='.$catid.'&pn='.$i.'">'.$i.'</a> &nbsp; ';
            }
        }
    }

    $paginationCtrls .= ''.$pagenum.' &nbsp; ';
    for($i = $pagenum+1; $i <= $last; $i++){
        $paginationCtrls .= '<a href="cat.php?catid='.$catid.'&pn='.$i.'">'.$i.'</a> &nbsp; ';
        if($i >= $pagenum+4){
            break;
        }
    }

    if ($pagenum != $last) {
        $next = $pagenum + 1;
        $paginationCtrls .= ' &nbsp; &nbsp; <a href="cat.php?catid='.$catid.'&pn='.$next.'">Next</a> ';
    }
}

?> 

和mysql查询的主要html代码:

 <div class="fp">
        <div class="filter">
            <b style="padding-left: 10px;">Filters:</b>
            <form class="filterform" action="" method="post"><br>
                Location: <br>
                <input name="location" ><br>
                Price Range:<br>
                Min:<input type="text" name="min" size="5"> Max:<input type="text" name="max" size="5"><br><br>
                <input class="submit-button" type="submit" name="filter" value="Filter">
            </form>
        </div>
        <div class="posts">
            <div id="adcat"><?php
                if(!empty($cat2)){
                    ?>
                    <a href="cat.php?catid=<?php echo $l1; ?>" ><?php echo $cat1." » "; ?></a><span><?php echo $cat2; ?></span>
                    <?php
                } else {
                echo "<font color='grey'>".$cat1."</font>";
                }
                        ?>

            </div>
            <div id="totalrez">
                <?php echo "Total: ".$rows; ?><br>
                <?php echo "Page".$pagenum." of ".$last; ?>
                </div>
            <br><br>
            <div id="detailformscat">
                    <?php
                    if(empty($p1) && empty($p2)){
                        $sql = "SELECT * FROM posts p JOIN images i ON p.id = i.postid WHERE p.id > 0 ";

                        if(!empty($location)){
                            $sql .= "AND location='$location'";
                        }
                        if(!empty($pricemin)){
                            $sql .= "AND price>='$pricemin' ";
                        }
                        if(!empty($pricemax)){

                            $sql .= "AND price<='$pricemax' ";
                        }
                        $sql .= "$limit";

                    } else if(!empty($p2)){
                        $sql = "SELECT * FROM posts p JOIN images i ON p.id = i.postid WHERE catid='$p2' ";
                        if(!empty($location)){
                            $sql .= "AND location='$location'";
                        }
                        if(!empty($pricemin)){
                            $sql .= "AND price>='$pricemin' ";
                        }
                        if(!empty($pricemax)){
                            $sql .= "AND price<='$pricemax' ";
                        }

                        $sql .= "$limit";
                    } else {
                        $sql = "SELECT * FROM posts p JOIN images i ON p.id = i.postid WHERE p.catid LIKE '$p1%' ";
                        if(!empty($location)){
                            $sql .= "AND location='$location'";
                        }
                        if(!empty($pricemin)){
                            $sql .= "AND price>='$pricemin' ";
                        }
                        if(!empty($pricemax)){
                            $sql .= "AND price<='$pricemax' ";
                        }

                        $sql .= "$limit";
                    }
                    $res = mysqli_query($connect,$sql);
                    $rows = mysqli_num_rows($res);

                     while ($row = mysqli_fetch_assoc($res)) {
                         $postid = $row["postid"];
                         ?>
                        <div id="ads">
                            <div id="adfavcat">
                                    <?php if(!isset($_SESSION["userid"])) { ?>
                                    <a href="post.php?login=false" ><img src="image/+fav.png" width="40px" height="40px"></a>
                                    <?php } else { 

                                            $userid = $_SESSION["userid"];
                                            $sql2 = "SELECT * FROM fav WHERE userid='$userid' AND postid='$postid' ";
                                            $res2 = mysqli_query($connect,$sql2);
                                            $rowcount = mysqli_num_rows($res2);

                                            if ($rowcount > 0){ ?>
                                                <a href="cat.php?catid=<?php echo $catid; ?>&delete&id=<?php echo $postid?>" ><img src="image/xfav.png" width="40px" height="40px"></a>
                                            <?php
                                            } else { ?>
                                            <a href="cat.php?catid=<?php echo $catid; ?>&add&id=<?php echo $postid; ?>" ><img src="image/+fav.png" width="40px" height="40px"></a>
                                            <?php }
                                    } ?>

                            </div>
                            <div id="titlepic">
                            <a href="review.php?id=<?php echo $postid; ?>"><?php echo $row["title"]; ?></a><br>
                            <a href="review.php?id=<?php echo $postid; ?>"><img src="<?php if(!empty($row["path1"])) { echo $row["path1"]; } else echo "image/noimage.png"; ?>" height="100px" width="150px"></a>
                            </div>
                            <div id="datescat">
                                <b>Date Added:</b> <?php echo date('m/d/Y H:i', $row["dateadded"]); ?><br>
                                <b>Renew Date:</b> <?php if($row["renewdate"] > 0){ echo date('m/d/Y H:i', $row["renewdate"]); } ?><br>
                                <b>Location:</b> <?php echo $row["location"]; ?><br>
                                <b>Price:</b> <?php echo $row["price"]."&pound"; ?><br>

                            </div>
                        </div>
                        <hr width="100%">
                        <?php
                     }

                ?>
                <div id="paginationctrl"><br>
                    <?php echo $paginationCtrls; ?>
                </div>
        </div>
          </div>
    </div>

我需要一个可能的解决方案,捕获后期数据,并保持在那里,直到你再次使用post方法或其他解决方案改变野兔,因为现在$_POST将等于null如果你通过分页的第二页

最佳答案 对于这种情况,您可以改为从$_POST更改为$_GET,因为这将更容易解决您的问题以及您将来可能遇到的任何问题.我认为大多数具有分页功能的网站都使用URL中的变量,因为它更容易跟踪.此外,您没有发送任何私人数据,所以我没有看到为什么不切换到其他方法的原因.您可以选择使用类似$_POST [‘page’]的内容,因此您可以检索其他行.

点赞