keep variables in pagination

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hi, i'm trying to set pagination for a search i run on my website, i'll
try to explain the easiest i can:

When i click the search button on search.php, data is received and
stored in variables within results.php,  MySQL structure seems to work
as expected, although i get some problems:

As soon as i click on the "Page 2" Link, i get nothing, neither results
nor page numbers.

I know why this happens: after clicking on any "Page X" link, the page
self-reloads and this changes the stored value in the VARIABLES (i
assume to 0 or nothing), therefore, MySQL structure doesn't work, since
there's no correct data to set the query.

Stating the obvious, if i set a fixed value instead of a variable my
structure perfectly works:

               -> Results are displayed
               -> Page numbers reflect the data stored in MySQL
               -> Page numbers are fully fonctional, i can click them
                   in any way, and it always work.

But i cannot set a fixed value since data is defined in search.php, so
results.php cannot have a fixed values, otherwise, the search is not a
search, it's only a fixed d

So my question is how do i keep the values received from the search.php
after every self-reload of the page.

This is the code i am using:


// Database Connection
    include 'db.php';


    $table = $_POST['table'];
    $data02 = $_POST['data02'];
    $data03 = $_POST['data03'];
    $data04 = $_POST['data04'];
    $data05 = $_POST['data05'];
    $data06 = $_POST['data06'];
    $data07 = $_POST['data07'];
    $data08 = $_POST['data08'];
    $data09 = $_POST['data09'];

// If current page number, use it if not, set one!

        $page = 1;
    } else {
        $page = $_GET['page'];

// Define the number of results per page

    $max_results = 10;

// Figure out the limit for the query based on the current page number.

    $from = (($page * $max_results) - $max_results);

// Perform MySQL query on only the current page number's results


    $sql = mysql_query("SELECT *  FROM `$table` WHERE `data02` LIKE
'%$data02%' AND `data03` LIKE '%$data03%' AND `data04` LIKE '%$data04%'
AND `data05` LIKE '%$data05%' AND `data06` LIKE '%$data06%' AND
`data07` >= $data08 AND `data07` <= $data09 ORDER BY `data07` ASC LIMIT
$from, $max_results");

           echo '<table id="cssstyle">'."\n";
    echo "<tr> <th>Top</th> <th>Top</th> <th>Top</th> <th>Top</th>
<th>Top</th> <th>Top</th> <th>Top</th> <th>Top</th>

        while($row = mysql_fetch_array($sql)){         // Build your formatted

            echo '<tr>'."\n"."<td align='left'>"."\n";
            echo $row['1strow'];
            echo "\n"."</td>"."\n"."<td align='center'>"."\n";
            echo $row['2ndrow'];
            echo "\n"."</td>"."\n"."<td align='center'>"."\n";
            echo $row['3rdrow'];
            echo "\n"."</td>"."\n"."<td align='center'>"."\n";
            echo $row['4throw'];
            echo "\n"."</td>"."\n"."<td align='center'>"."\n";
            echo $row['5throw'];
            echo "\n"."</td>"."\n"."<td align='center'>"."\n";
            echo $row['6throw'];
            echo "\n"."</td>"."\n"."<td align='center'>"."\n";
            echo $row['7throw'];
            echo "\n"."</td>"."\n"."<td width='28%' align='left'>"."\n";
            echo $row['8throw'];
            echo "\n"."</td>"."\n"."<td align='center'>"."\n";
            echo $row['9throw'];
            echo "</a>"."\n"."</td>"."\n"."</tr>"."\n"."\n";
            echo "</table>"."\n"."</div>"."\n"."<!-- Results-wrapper Ends

// Figure out the total number of results in DB:


    $total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM

// Figure out the total number of pages. Always round up using ceil()
    $total_pages = ceil($total_results / $max_results);

// Build Page Number Hyperlinks
    echo "<div id='pages'><center>Select a Page<br />";

// Build Previous Link
    if($page > 1){
        $prev = ($page - 1);
        echo "<a
href=\"".$_SERVER['PHP_SELF']."?page=$prev\">&lt;&lt;Previous</a> ";

    for($i = 1; $i <= $total_pages; $i++){
        if(($page) == $i){
        echo "$i ";
        } else {
        echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> ";

// Build Next Link
    if($page < $total_pages){
        $next = ($page + 1);
        echo "<a
    echo "</center>"."\n";


I hope you can help me, i'm don't master php, but i understand a lot of
it; I'm actually thinking that what might solve my problem will come
from something like:

But that's exactly what i don't know how to do U_U

Thanks, and i hope you could help me


Re: keep variables in pagination

Quoted text here. Click to load it

Take a look at using sessions:

They allow you to store a value in one script, and then access it in later  
scripts.  They're very easy to use, and might be what you need.


Re: keep variables in pagination

You could pass them in the query string, but that is cumbersome, You
could also put all the variables in to hidden inputs, then instead of
making the next page link href directly to the next page have it do a
javascript form.submit.

Re: keep variables in pagination

Quoted text here. Click to load it

why not just simplify this to...
echo "\n</td>\n<td align='center'>\n$row[2ndrow]";
that other way looke like the hard way to do things.  And if you need double  
quotes in the string, just use \"

Quoted text here. Click to load it

Re: keep variables in pagination

Quoted text here. Click to load it

Hi Jim, thanks for the advice, i've have changed everything in my
source code so it's more "legible" now :)

I'm new with PHP and i've learned everything i know from tutorials on
the net, the problem with them is that sometimes, little things (like
', \t, \n \") are overseen and ppl ignore them since they are rookies
mistakes, but rookies (like me) well, don't know about them and we,
actually, make those mistakes :S

Thanks again for the tip :D

I have a question though, what's better to use (in a very general way)
SESSIONS or COOKIE? I mean i worked already with cookies and everything
works but if i need to learn one or the other, what would be the best?


Re: keep variables in pagination

Quoted text here. Click to load it

Every time you create a cookie, you are sending more crapola over to the
client computer, to be stored on their hard disk, and ALL of your cookies
are transmitted back to you with EVERY http request the browser makes.

With a session, PHP basically sends one cookie, and then uses that cookie
to look up information that is stored locally, on your server.  The
bandwidth impact is much lower.
- Tim Roberts,
  Providenza & Boekelheide, Inc.

Re: keep variables in pagination

Hi Tim, thanks for clarifying, i guess that switching from cookies to
sessions isn't a big mess, is it? I mean, I would only have to change
the code and settings related to $_COOKIE for $_SESSION??

What i meant is like in results.php i have $foo =
$_COOKIE['something']; i should change that for $foo =
$_SESSION['something']; obviously, setting the session_start and stuff
like that in the respective pages (the one the stores the info and the
one that retrieves it, am i right?


Quoted text here. Click to load it

Re: keep variables in pagination

i haven't read through it ,but i wish something below can be helpful!

if(empty($_SESSION['search_sql']) && isset($_POST[submit])){
     // to construct search_sql
     $search_sql = ......
     $SESSION[search_sql] = $search_sql;
     $search_sql = $_SESSION[search_sql];

$per_page = 20;
$start = ($pageno-1)*$per_page;

$query = mysql_query($search_sql." LIMIT ".$start.",".$per_page,$link);

Re: keep variables in pagination

$_POST variables are only posted at one request, they are neither a
cookie or a session.

try using sessions :)

Re: keep variables in pagination

Well, i started working with cookies, what i did to solve the problem
is the following:

Data is sent from page.php into search.php which stores the information
in cookies and this page forwards to results.php

-> Data from page.php is sent with a search box that uses POST.
-> Data received from page.php into search.php is taken with
-> Data in results.php is retreived with $_COOKIE['table']

Do you think this is fine or should i look for something more direct?
what i mean is straight from page.php into results.php.

Everything is working PERFECTLY with this structure, i'm just wondering
if it's practical or if i'm probably repeating a step that i could

Thank you all!

Re: keep variables in pagination

Quoted text here. Click to load it

That URL there is what you would use $_GET[] to get your form variables  
Even if your form method is set for post, you can still use get, but you  
will notice that when people hit the back button on their browser your old  
variables will come back on you, so be picky about when you use the url for.  
Actually, I think it can also repost old form data too, especially if the  
user hits refresh.
Use $_POST for post form vars of course.
You also have the option of using $_SESSION[] to keep data between  
pages, which is a good way to hide it.

Quoted text here. Click to load it

Site Timeline