如何在分页中获取帖子值

2022-01-04 00:00:00 sql php pagination

我正在使用 php 中的分页进行搜索功能.下面是我的代码,但是每当我单击下一个链接时,搜索查询都不会采用通过 POST 传递的变量.你能帮我吗..

searchstr=$_POST['q'];session_start();$_SESSION['searchchar']=$searchstr;$rec_limit=3;$connection=connection();$searchstr=$_REQUEST['q'];$sql="Select count(*) from FIMTRN_Memorial where FirstName like '%".$_SESSION['searchchar']."%'";回声$sql;$result1=mysql_query($sql);如果(!$result1){die('无法获取数据:' .mysql_error());}$row = mysql_fetch_array($result1,MYSQL_NUM);$rec_count = $row[0];回声$rec_count;if( isset($_GET{'page'} ) ){$page = $_GET{'page'} + 1;$offset = $rec_limit * $page ;}别的{$页= 0;$offset = 0;}$left_rec = $rec_count - ($page * $rec_limit);$sql="Select * from FIMTRN_Memorial where FirstName like '%".$_SESSION['searchchar']."%' limit $offset,$rec_limit";回声$sql;$result=mysql_query($sql);$rec_num=mysql_num_rows($result);如果($rec_num>0){while($row = mysql_fetch_array($result,MYSQL_ASSOC)){$fname=$row['名字'];$image=$row['ProfileImagePath'];$about=$row['关于'];$url=$row['CustomURL'];$DOB=$row['DOB'];$DOD=$row['DOD'];?><div class="搜索"><p class="content"><img src="<?php echo $image;?>"样式=边框:1px纯红色;宽度:100像素;高度:100像素;"/></p><div class="content"><p><?phpecho "<a href="$url;" target="_blank">$fname</a>";?><p>(<?php echo date("Y",strtotime($DOB));?>-<?php echo date("Y", strtotime($DOD));?>)<;/p></p><p><?php echo$about?></p>

<?php}if( $page > 0 & $left_rec > $rec_limit){$last = $page - 2;echo "<a href="?page=$last">以前的记录</a> |";echo "<a href="?page=$page">下一条记录</a>";}else if( $page == 0 & $rec_count>$rec_limit ){echo "<a href="?page=$page">下一条记录</a>";}否则 if( $left_rec <$rec_limit & $rec_count>$rec_limit ){$last = $page - 2;echo "<a href="?page=$last">以前的记录</a>";}}别的{echo"没有找到纪念馆,请换其他名字试试.";}在这里输入代码?>

解决方案

您的代码中有几个问题.

首先:

您错误地使用了 $_GET 变量.你有 $_GET{'page'},但你应该像使用 $_POST 变量一样使用它,像这样: $_GET['page']

第二:

您错误地理解了发布和点击链接的想法.您需要使用下一个和上一个按钮制作一个隐藏表单,其中包含您需要的所有其他变量.例如,不涉及更改大量其他代码的最简单方法:

if( $page > 0 & $left_rec > $rec_limit){$last = $page - 2;//上一个按钮表单echo "
";echo "<input type='hidden' name='q' value='" .$_POST['q'] ."'/>";//这将是一个保存信息但不会出现在页面上的字段(尽管用户如果查看源代码就可以看到它)echo "<input type='submit' value='Previous Records'/>";echo "</form>";回声|";//您正在使用的分隔符,如果这是您想要的,您需要将输入 type='submit's 设置为看起来更像一个链接//下一个按钮表单echo "";echo "<input type='hidden' name='q' value='" .$_POST['q'] ."'/>";//这将是一个保存信息但不会出现在页面上的字段(尽管用户如果查看源代码就可以看到它)echo "<input type='submit' value='Next Records'/>";echo "</form>";}

此方法将在请求中保留您的变量.使用链接实质上是重置 $_POST 变量.有很多其他可能的解决方案,但这是修改示例所需的工作量最少的一个.

I am working on search functionality with pagination in php.Below is my code but whenever I click on next link the search query is not taking variable which is passed through POST. Can you please help me..

searchstr=$_POST['q'];
session_start(); 
$_SESSION['searchchar']=$searchstr;
$rec_limit=3;
$connection=connection();
$searchstr=$_REQUEST['q'];
$sql="Select count(*) from  FIMTRN_Memorial where FirstName like '%".$_SESSION['searchchar']."%'";
echo$sql;
$result1=mysql_query($sql);
if(! $result1)
{
    die('Could not get data: ' . mysql_error());
}
$row = mysql_fetch_array($result1,MYSQL_NUM );
$rec_count = $row[0];
echo$rec_count;
if( isset($_GET{'page'} ) )
{
    $page = $_GET{'page'} + 1;
    $offset = $rec_limit * $page ;
}
else
{
    $page = 0;
    $offset = 0;
}
$left_rec = $rec_count - ($page * $rec_limit);
$sql="Select * from  FIMTRN_Memorial where FirstName like '%".$_SESSION['searchchar']."%' limit $offset,$rec_limit";
echo$sql;
$result=mysql_query($sql);
$rec_num=mysql_num_rows($result);
if($rec_num>0)
{
    while($row = mysql_fetch_array($result,MYSQL_ASSOC))
    { 
        $fname=$row['FirstName'];
        $image=$row['ProfileImagePath'];
        $about=$row['About'];
        $url=$row['CustomURL'];
        $DOB=$row['DOB'];
        $DOD=$row['DOD'];
?>
<div class="search">
    <p class="content"><img src="<?php echo $image;?>" style="border:1px solid red;width:100px;height:100px;" /></p>
<div class="content"><p>
<?php 
    echo "<a href="$url;" target="_blank">$fname</a>";
?>
    <p>(<?php echo date("Y",strtotime($DOB));?>-<?php echo date("Y", strtotime($DOD));?>)</p>
    </p><p><?php echo$about?></p>
</div>
</div>
<?php
    }
    if( $page > 0 & $left_rec > $rec_limit)
    {

        $last = $page - 2;
        echo "<a href="?page=$last">Previous Records</a> |";
        echo "<a href="?page=$page">Next  Records</a>";
    }
    else if( $page == 0 & $rec_count>$rec_limit )
    {     
        echo "<a href="?page=$page">Next  Records</a>";
    }
    else if( $left_rec < $rec_limit & $rec_count>$rec_limit )
    {
        $last = $page - 2;
        echo "<a href="?page=$last">Previous Records</a>";
    }
}
else
{
     echo"No memorial found.Please try with other name.";
}



enter code here

?>

解决方案

There are a couple problems in your code.

First:

You are using the $_GET variable incorrectly. You have $_GET{'page'}, but you should be using it the same way as the$_POST variable, like this: $_GET['page']

Second:

You are interpreting the idea of POSTing and clicking links incorrectly. What you need to make with your next and previous buttons are a hidden form that has all the other variables you need. For example, the simplest way that won't involve changing a lot of your other code:

if( $page > 0 & $left_rec > $rec_limit)
{
    $last = $page - 2;
    // Previous button form
    echo "<form action='?page=$last' method='POST'>";
    echo "<input type='hidden' name='q' value='" . $_POST['q'] . "' />"; // This will be a field that saves information but won't appear on the page (though a user could see it if they view source)
    echo "<input type='submit' value='Previous Records' />";
    echo "</form>";
    echo " | "; // The delimiting character you were using, you'll need to style the input type='submit's to look more like a link if that's what you are going for
    // Next button form
    echo "<form action='?page=$page' method='POST'>";
    echo "<input type='hidden' name='q' value='" . $_POST['q'] . "' />"; // This will be a field that saves information but won't appear on the page (though a user could see it if they view source)
    echo "<input type='submit' value='Next  Records' />";
    echo "</form>";
}

This method will keep your variables in the request. Using a link essentially resets the $_POST variable. There are a ton of other possible solutions, but this is one will require the least amount of work to modify your example.

相关文章