0

我有一个值通过 $_POST 传递到我的搜索结果页面并存储在一个名为 $term 的变量中。然后该变量在 MySQL 查询中,然后运行以显示搜索结果,如果有超过 15 个结果,我有一个分页 php 脚本,它将结果分成几页。

第一页很棒,找到了正确数量的结果,并且提供了正确数量的页面。如果我回显变量,则正确的值将存储在其中。但是,如果我选择查看下一批结果并且页面刷新,则列出表的所有行(好像查询只是 SELECT * 没有参数)并且变量现在为空

这是我的代码(它有点长,抱歉,我打算把它放在另一个页面中,并在它正常工作时包含它)

    <?php
$tbl_name="accounts";
$adjacents = 3;


if (isset($_POST['basic_search_submit'])){
$term = $_POST['item'];}

$query = "SELECT COUNT(*) as num FROM accounts LEFT JOIN accounts_cstm ON accounts.id = accounts_cstm.id_c  WHERE accounts.name LIKE '%$term%' ORDER BY accounts.name";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];

    $targetpage = "search_results_play.php";    
$limit = 15;                                
$page = $_GET['page'];
if($page) 
    $start = ($page - 1) * $limit;          
else
    $start = 0;                             
    $sql = "SELECT * FROM accounts LEFT JOIN accounts_cstm ON accounts.id = accounts_cstm.id_c WHERE accounts.name LIKE '%$term%' ORDER BY accounts.name LIMIT $start, $limit";
$result = mysql_query($sql);
if ($page == 0) $page = 1;                  
$prev = $page - 1;                          
$next = $page + 1;                          
$lastpage = ceil($total_pages/$limit);      
$lpm1 = $lastpage - 1;                      
$pagination = "";

if($lastpage > 1)
{   
    $pagination .= "<div class=\"pagination\">";
            if ($page > 1) 
        $pagination.= "<a href=\"$targetpage?page=$prev\">&#60; &#60; previous</a>";
    else
        $pagination.= "<span class=\"disabled\">&#60; &#60; previous</span>";   

            if ($lastpage < 7 + ($adjacents * 2))
    {   
        for ($counter = 1; $counter <= $lastpage; $counter++)
        {
            if ($counter == $page)
                $pagination.= "<span class=\"current\">$counter</span>";
            else
                $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                 
        }
    }
    elseif($lastpage > 5 + ($adjacents * 2))    //enough pages to hide some
    {
                    if($page < 1 + ($adjacents * 2))        
        {
            for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
            {
                if ($counter == $page)
                    $pagination.= "<span class=\"current\">$counter</span>";
                else
                    $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                 
            }
            $pagination.= "...";
            $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
            $pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";       
        }
                    elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
        {
            $pagination.= "<a href=\"$targetpage?page=1\">1</a>";
            $pagination.= "<a href=\"$targetpage?page=2\">2</a>";
            $pagination.= "...";
            for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
            {
                if ($counter == $page)
                    $pagination.= "<span class=\"current\">$counter</span>";
                else
                    $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                 
            }
            $pagination.= "...";
            $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
            $pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";       
        }

        else
        {
            $pagination.= "<a href=\"$targetpage?page=1\">1</a>";
            $pagination.= "<a href=\"$targetpage?page=2\">2</a>";
            $pagination.= "...";
            for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
            {
                if ($counter == $page)
                    $pagination.= "<span class=\"current\">$counter</span>";
                else
                    $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                 
            }
        }
    }


    if ($page < $counter - 1) 
        $pagination.= "<a href=\"$targetpage?page=$next\">next &#62; &#62;</a>";
    else
        $pagination.= "<span class=\"disabled\">next &#62; &#62;</span>";
    $pagination.= "</div>\n";       
}
?>

  <? while ($row = mysql_fetch_array($result)){
 //table displaying results
 }
?>
<?=$pagination?>

任何帮助将不胜感激,谢谢

4

2 回答 2

3

PHP 中的变量不会跨页面持久存在。当然其中一些除外,但经常性的$page还是$whatever不会坚持的。您将加载一个页面,其中包含一些变量,然后在页面在浏览器中完全加载后,这些变量将消失。

建议session_start()在脚本顶部、所有页面中使用,并用于$_SESSION['variable']=...存储变量。这样,它们将跨页面持续存在。

最简单的修复:

$page = $_GET['page']; 

将该行替换为:

$page = isset($_SESSION['page']) && is_numeric($_SESSION['page']) ? $_SESSION['page'] : 0;
$_SESSION['page'] = $page;

session_start()在每个脚本的顶部添加。如果你有一个共同的头包含,或带有 mysql 身份验证数据的 config.php,请将其放在该文件的顶部。

于 2012-07-24T20:53:23.360 回答
0

另一种解决方案是使用GET请求进行搜索并通过获取术语$_GET['term']并附加到生成的链接'&term='. $_GET['term']。这很容易做到。usin 的主要缺点$_SESSION['term']是,例如,如果您将页面留给主页,则此变量将保持设置状态直到会话到期,并且可能会在将来出现一些麻烦。

于 2012-10-19T01:10:40.047 回答