Jump to content

How to keep variable in pagination


marukochan

Recommended Posts

Hope somebody could help me here.

 

I have a script which the page will display 5 records per page. The first page was OK but nothing appears when I click the 2nd page onwards.

 

I'll cut short the code.

 

project_option.php

<?php
$title = "Project Database Menu";
include ("page_function.php");

header_start();
header_user();
echo ("<p><b>Project Menu</b></p>");
?>

<form action="project_db.php" method="post">
<table width="606" border="0">
  <tr>
    <td width="32"><div align="center">1.</div></td>
    <td width="564"><strong>View</strong></td>
  </tr>
  <tr>
    <td>
      <div align="center">
        <input type="radio" name="option" value="all">
        </div></td>
    <td><em>All Projects </em></td>
  </tr>
  <tr>
    <td>
      <div align="center">
        <input type="radio" name="option" value="status">
        </div></td>
    <td><em>By Status : <select name="status"><option value="">--Select--</option>
<option>Bidding</option>
<option>Awarded</option></select></em></td>
  </tr>
  <tr>
    <td>
      <div align="center">
        <input type="radio" name="option" value="datebid">
        </div></td>
....
....
....

Here, the user will choose which project to choose (all, by status, by date ....)

 

Then project_db.php will get the user's option to display the record.

 

project_db.php

<?php
$title = "Project Database Page";
include ("page_function.php");
include ("option_process2.php");

header_start();
header_user();
echo ("<p><b>Project List</b></p>");

connect_database();

$option =$_POST['option'];

switch ($option){
case "all":
display_all();
break;
case "status":
display_status();
break;
}

footer();
?>

 

display_all() is the function to display 5 records per page for all projects.

 

The pagination function is OK already. Only that when I click next page onwards, there is no display. When I echo $option, $option is empty. Meaning that when I refresh the page to go to next, the $option variable becomes empty. How do I keep the value of this $option.

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/38115-how-to-keep-variable-in-pagination/
Share on other sites

Looks like you simply need to put in a way for display_all() to know what page they are on.

 

I would do something like this

 

<?php
function display_all($start, $end)

{
  //For error checking
  if (empty($start) || empty($end))
      $displayRange = "LIMIT 0, 5";
  else
      $displayRange = "LIMIT $start, $end";
  
  // Change your query to show the new limit range
  $query = "SELECT * FROM table $displayRange";

  //  Rest of your code goes here
}

//Next change the value of the link to reflect a start stop range or set a defalt value
switch ($option){
case "all":

if (empty($_GET['start']))
	{
	$start = 0;
	$end = 5;
	$linkStart = 6;
	$linkEnd = 10;
	}
else
	{
	$start = $_GET['start'];
	$end = $_GET['start'];
	$linkStart = $start + 5;
	$linkEnd = $end + 5;
	}

             display_all($start, $end);
             break

 

Finally on your link you need to add the start - stop range.

 

<a href="myscript.php?$option=all&start=$linkStart&end=$linkEnd">Next Page</a>

Thank you for replying lachild, but that doesn't work either. My guess is that when the page reloads, $option will be empty and the 'switch' doesn't know which case to run.

 

I have been surfing the internet hoping someone somewhere has this kind of problem before but I still failed to find any. It is similar to pagination and search variables problems that I have looked through in this forum but I still cannot get how to do it.  :-\

that should work, works for me

 

did you remember to define each of your vars as $_GET

 

$option="$_GET[option]; etc..

 

 

Looks like you simply need to put in a way for display_all() to know what page they are on.

 

I would do something like this

 

<?php
function display_all($start, $end)

{
  //For error checking
  if (empty($start) || empty($end))
      $displayRange = "LIMIT 0, 5";
  else
      $displayRange = "LIMIT $start, $end";
  
  // Change your query to show the new limit range
  $query = "SELECT * FROM table $displayRange";

  //  Rest of your code goes here
}

//Next change the value of the link to reflect a start stop range or set a defalt value
switch ($option){
case "all":

if (empty($_GET['start']))
	{
	$start = 0;
	$end = 5;
	$linkStart = 6;
	$linkEnd = 10;
	}
else
	{
	$start = $_GET['start'];
	$end = $_GET['start'];
	$linkStart = $start + 5;
	$linkEnd = $end + 5;
	}

             display_all($start, $end);
             break

 

Finally on your link you need to add the start - stop range.

 

<a href="myscript.php?$option=all&start=$linkStart&end=$linkEnd">Next Page</a>

Opps there was a small problem with the code..  In my example I set end to the start value

 

$end = $_GET['start'];

 

Hope you caught this...  if not it should read

 

$end = $_GET['end'];

 

also in my example the option string is set via get as the previouse poster pointed out.  If you are setting this varriable using post you can still use this type of option by just checking where the option value is set.. so something like below should work.

 

In your example you have this:

$option =$_POST['option'];

 

Since the next page is a link that is not attached to a form the best way to set this on the link is through get.  Otherwise you would have to build a blank form with a hidden option for the option varriable and use JavaScript to submit the page, which is just alot of extra work.  So instead I'd recommend changeing that line to something like this:

 

if (empty($_POST['option']))
  $option = $_GET['option'];
else
  $option = $_POST['option'];

Now the rest of the code should work.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.