Jump to content

[SOLVED] passing a variable to paginated query results


Recommended Posts

I have a form that passes a single variable via the GET function to a mysql databse query with the results paginated.  The first page of the results work fine, but the link to the second page yields the page 2 results of the entire table.  I'm guessing that the variable hasn't passed to page2 query?  Please excuse my ignorance.  Here's my code:

 

<?php

//where $rory is the variable that is passed via the form with the GET action

$page = $_GET['page']

$rory=$_GET['rory'];

mysql_connect("localhost","<user>","<password>") or die(mysql_error());

mysql_select_db("<db>") or die(mysql_error());

$limit                  = 10;      // Change to how many results you want per page             

        $query_count        = "SELECT * FROM bootlist WHERE venue LIKE '%$rory%' || location LIKE '%$rory%' || cd1 LIKE '%$rory%' || cd2 LIKE '%$rory%' || date LIKE '%$rory%' ORDER BY date";   

        $result_count  = mysql_query($query_count); 

           

        $totalrows          = mysql_num_rows($result_count);

        if(empty($page)){

                $page = 1;

        }

                 

        $limitvalue = $page * $limit - ($limit);

$query = ("SELECT * FROM bootlist WHERE venue LIKE '%$rory%' || location LIKE '%$rory%' || cd1 LIKE '%$rory%' || cd2 LIKE '%$rory%' || date LIKE '%$rory%' ORDER BY date LIMIT $limitvalue, $limit");

$result = mysql_query($query);

$num=mysql_num_rows($result);

if(mysql_num_rows($result) == 0){

                echo("Nothing to Display!");

        }

 

 

 

while ($row = mysql_fetch_array ($result) ) {

echo "Date: ".$row['date'];

echo "<br>Venue: ".$row['venue'];

echo "<br>Location: ".$row['location'];

echo "<br>CD 1: " .$row['cd1'];

echo "<br>CD 2: " .$row['cd2'];

echo "<hr>";

}

 

if($page != 1){

                $pageprev = $page-1;

                 

                echo("<a href=\"$PHP_SELF?page=$pageprev\">PREV ".$limit."</a>   ");

        }else{

                echo("PREV " .$limit."   ");

        }

        $numofpages = $totalrows / $limit;

         

        for($i = 1; $i <= $numofpages; $i++){

                if($i == $page){

                        echo($i." ");

                }else{

                        echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");

                }

        }

 

        if(($totalrows % $limit) != 0){

                if($i == $page){

                        echo($i." ");

                }else{

                        echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");

                }

        }

        if(($totalrows - ($limit * $page)) >=0){

                $pagenext = $page+1;

                 

                echo("   <a href=\"$PHP_SELF?page=$pagenext\">NEXT ".$limit."</a>");

        }else{

                echo("   NEXT " .$limit);

        }

         

        mysql_free_result($result);

 

 

?>

 

Your code is long and a bit hard to read because you didn't use [ code ] tags, but maybe this is the problem:

echo("<a href=\"$PHP_SELF?page=$pageprev\">PREV ".$limit."[/url]   ");

I think this should be

echo("<a href=\"$PHP_SELF?page=$pageprev&rory=".$_GET['rory']."\">PREV ".$limit."[/url]   "); 

 

And similarily for the other parts where you echo the 'next 10' links, etc....

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.