tatty27
tatty27

Reputation: 1554

PHP pagination loses variable on other pages

I have a value that is passed to my search results page via $_POST and stored in a variable called $term. The variable is then in a MySQL query which then runs to display the search results and if there are more than 15 results I have a pagination php script which breaks the results up into several pages.

The first page is great, the correct number of results are found and the correct number of pages are available. If I echo the variable the correct value is stored in it. However, if I chose to look at the next lot of results and the page refreshes all the rows of the table are listed (as if the query was just SELECT * with no argument) and the variable is now empty

Here is my code (it's a bit long, sorry, I intend to put a big chuck of it away in another page and include it once it's working correctly)

    <?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?>

Any help would be greatly appreciated, thanks

Upvotes: 0

Views: 3404

Answers (2)

Reflective
Reflective

Reputation: 3917

the other solution is to use GET request for search and get the term by $_GET['term'] and append to the generated links '&term='. $_GET['term']. This would be done easy. The main disadvantage of usin $_SESSION['term'] is that if you leave the page to homepage for example, this variable will stay set until the session expire and could case some future troubles.

Upvotes: 0

Silviu-Marian
Silviu-Marian

Reputation: 10907

Variables in PHP do not persist across pages. Except for some of them of course, but regular $page or $whatever will not persist. You will load a page, have some vars in it, then after the page is fully loaded in the browser, those variables will be gone.

The recommendation is to use session_start() at the top of your scripts, in all pages, and use $_SESSION['variable']=... to store variables. This way, they will persist across pages.

The easiest fix:

$page = $_GET['page']; 

Replace that line with this:

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

And add session_start() at the top of every script. If you have a common head include, or config.php with mysql authentication data, put it at the top of that file.

Upvotes: 3

Related Questions