gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 23:31:11 27 4
gpt4 key购买 nike

你好,我刚刚完成分页,我的过滤系统出现问题,我使用 from 过滤并使用 $_POST 提取数据,但问题是当我转到第二页时,帖子意味着什么都没有,我丢失了一些数据然后在我的情况下我得到更多然后我过滤它,我的意思是如果我按位置过滤(伦敦)我得到 36 中的 32 但是当我转到第二页时我将得到所有 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 将等于 null

最佳答案

对于这种情况,您可以将 $_POST 更改为 $_GET,因为这样会更容易解决您的问题以及您将来可能遇到的任何问题。我认为大多数带有分页的网站都在 URL 中使用变量,因为它更容易跟踪。此外,您没有发送任何私有(private)数据,所以我看不出为什么不切换到其他方法的原因。您可以选择使用 $_POST['page'] 之类的选项,并据此检索其他行。

关于php - 分页和$_post含义的解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37293093/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com