【问题标题】:How get post value in pagination如何在分页中获得帖子价值
【发布时间】:2014-02-20 21:21:14
【问题描述】:

我正在使用 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']."%'";
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

?>

【问题讨论】:

  • 你不能在 url 上将值从一个页面传递到另一个页面,你可以使用 $_GET 发送搜索字符串
  • 请从代码中删除不需要的` 并正确对齐您的代码。

标签: php sql pagination


【解决方案1】:

您的代码中有几个问题。

第一:

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

第二:

您正在错误地解释发布和单击链接的想法。您需要使用下一个和上一个按钮创建一个隐藏表单,其中包含您需要的所有其他变量。例如,不需要更改大量其他代码的最简单方法:

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>";
}

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

【讨论】:

  • 也许不是真正的“问题”,您设置了两次$searchstr,一次设置为$_POST['q'],一次设置为$_REQUEST['q']
  • 感谢您的回复。如果我删除 $_REQUET['q'] 也不起作用。
  • 嗨...我在搜索字符串中使用'引号然后它不会传递到下一页..任何人都可以帮助我..谢谢
【解决方案2】:

如果您要创建排序和搜索功能,我建议您使用$_SESSION 变量而不是$_POST$_GET,它将帮助您很好地管理您的网格/数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-03
    • 1970-01-01
    • 2020-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多