There are numerous articles out there about pagination and just as many different approaches to achieving pagination of mysql data using php.
I do not intend to add another pagination script to the plethora of existing php pagination scripts but what I will do is add a simple piece of script that is often omitted from pagination scripts. A simple script yet invaluable for passing data across paginated webpages. A good pagination script will not need this hack but I have posted the solution in response to a call for help.
Scenario
You already have a mysql database set up and are happy with inserting, updating, deleting data to your mysql database.
You know how to retrieve mysql results with php and display the results using tables and so on.
You have found/written a php pagination script that handles your query and it all works well until….
you use a form to send information to your paginated page, it displays the first page list of results but not the second or third or… so on.
The problem
The problem is caused because you have not sent the Form GET – POST information onto the subsequent paginated pages (I know you scream, but how do I fix this).
A simple way to fix this is to:
- take the results from your form (whether they are sent with POST or GET)
- make them a variable
- generate a cookie
- make the cookie a variable you can work with
- provide the ability to update the cookie if you send new post data to the paginated page (from a drop down form for example)
The Script
PHP Code:
if(isset($_GET['Your_Form_Input']))
{
$getforminput=$_GET['Your_Form_Input'];
setcookie(“somecookie”,$getforminput, time()+240);
$output_variable = $_GET['Your_Form_Input'];
}
if(!isset($_GET['Your_Form_Input']))
{
$output_variable = $_COOKIE['somecookie'];
}
So what does all this mean then?
Well….
PHP Code:
if(isset($_GET['Your_Form_Input']))
First we use the if construct to identify if a certain situation exists, in this case we want to identify whether an input has been received from a form.
further info on IF here
isset is used to determine whether a variable has been set (think is it set? – isset). In this case we are checking if the page has recieved a form input.
further info on isset here
Now, if the situation is true then we will make an output inside our curly braces { to start and } to end.
PHP Code:
{
$getforminput=$_GET['Your_Form_Input'];
setcookie(“somecookie”,$getforminput, time()+240);
$output_variable = $_GET['Your_Form_Input'];
}
The above code defines our output to execute once our if statement is true.
Line by Line:
We have now determined that our input has been received from a form and is therefore a NEW input.
PHP Code:
$getforminput=$_GET['Your_Form_Input'];
Here we are simply making our form output into a variable that we can use later (to influence our mysql results for example)
PHP Code:
setcookie(“somecookie”,$getforminput, time()+240);
We now want to transfer our variable into a cookie so that is can be used on subsequant pages (for pagination in this example). In this example I have set the cookie name to somecookie, asigned the value as our variable $getforminput and set the cookie to expire in 4 minutes (240 seconds)
further info on Setting Cookies here
PHP Code:
$output_variable = $_GET['Your_Form_Input'];
Okay, now the part which may cause some confusion. Hopefully this will be clear enough, if not join the forum and post a message – please note that you have to contact us to request membership (to stop spammers )
We now assign another variable which can be used in your mysql query to retrieve specific mysql results). In this instance we are setting our output variable (name it what you want) to reflect the value of our form input.
i.e.
Code:
SELECT * FROM your_table WHERE some_column = ‘$output_variable’
So, the above code dealt with handling the paginated results when the information was received from a form. Now we will look at what to do when we move onto our second, third, etc paginated page and the form data is no longer present.
Second part of the code
PHP Code:
if(!isset($_GET['Your_Form_Input']))
{
$output_variable = $_COOKIE['somecookie'];
}
In the first part of our code we captured the information from our form, made this a variable, assigned it to a cookie and assigned a variable for our mysql query (based on our form input).
In the second part of our code, we again use the if and isset functions to determine whether a post has been made. Note the slight difference in the isset line, isset is now prefixed with a ! – this changes the command from IS SET to IS NOT SET.
In the first part of the script we defined what should happen if a result was received from a form, in the second part of the script we are defining what should happen if a result is NOT received from a form.
In this case we use the cookie we created in part one as our variable value for our Mysql query. This enables us to pass the information from our form across several pages until:
- the cookie expires (time based)
- the cookie value is changed by a new form input
Summary
This is a very simple way to allow information to be passed across paginated pages in php. There are alternative options using if and else and elseif. This article has simply provided a simple solution which should allow a wider audience to pass the hurdle that is created by using form inputs on php pagination pages.
If you do find this article useful, I always appreciate a link back to the site – Thanks
Code:
<a href=”http://www.peterstarmer.com/”> Peter Starmer – A myriad of thoughts</a>
What others think…