user970638
user970638

Reputation: 129

PHP: Persistent POST Variable Across Pagination Pages

After hours of trying to figure out why my pagination script wasn't working with my variables, I figured out with the help of some of you that my POST variables aren't being carried over from page to page as my data paginates.

Are sessions the best way to handle this type of scenario? For example, I have this:

session_start(); //start session
$_SESSION["formdata"] = $_POST['City']; //get form data input
$myvar = $_SESSION["formdata"]; 
$sql = "SELECT * FROM $tbl_name WHERE City = \"$myvar\" LIMIT $start, $limit"; //query

Regardless, this does not seem to be working. The form data is displayed on the first page but any subsequent pages are blank. Thanks for the help and advice

CODE:

session_start();
	
   
	
	include('sqlconnect.php');	// include your code to connect to DB.
mysql_select_db("barter") or die(mysql_error());
	$tbl_name="posts";		//your table name
	// How many adjacent pages should be shown on each side?
	$adjacents = 3;
	
	/* 
	   First get total number of rows in data table. 
	   If you have a WHERE clause in your query, make sure you mirror it here.
	*/
	$query = "SELECT COUNT(*) as num FROM $tbl_name";
	$total_pages = mysql_fetch_array(mysql_query($query));
	$total_pages = $total_pages[num];
	
	/* Setup vars for query. */
	$targetpage = "fp2.php"; 	//your file name  (the name of this file)
	$limit = 3; 								//how many items to show per page
	$page = $_GET['page'];
	if($page) 
		$start = ($page - 1) * $limit; 			//first item to display on this page
	else
		$start = 0;								//if no page var is given, set start to 0
	
	/* Get data. */
	
	$_SESSION["formdata"] = $_GET['City'];
	$myvar = $_SESSION["formdata"];
	echo $myvar;
	$sql = "SELECT * FROM $tbl_name WHERE City = \"$myvar\" LIMIT $start, $limit";
	$result = mysql_query($sql);
	
	/* Setup page vars for display. */
	if ($page == 0) $page = 1;					//if no page var is given, default to 1.
	$prev = $page - 1;							//previous page is page - 1
	$next = $page + 1;							//next page is page + 1
	$lastpage = ceil($total_pages/$limit);		//lastpage is = total pages / items per page, rounded up.
	$lpm1 = $lastpage - 1;						//last page minus 1
	
	/* 
		Now we apply our rules and draw the pagination object. 
		We're actually saving the code to a variable in case we want to draw it more than once.
	*/
	$pagination = "";
	if($lastpage > 1)
	{	
		$pagination .= "<div class=\"pagination\">";
		//previous button
		if ($page > 1) 
			$pagination.= "<a href=\"$targetpage?page=$prev\">previous</a>";
		else
			$pagination.= "<span class=\"disabled\">previous</span>";	
		
		//pages	
		if ($lastpage < 7 + ($adjacents * 2))	//not enough pages to bother breaking it up
		{	
			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
		{
			//close to beginning; only hide later pages
			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>";		
			}
			//in middle; hide some front and some back
			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>";		
			}
			//close to end; only hide early pages
			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>";					
				}
			}
		}
		
		//next button
		if ($page < $counter - 1) 
			$pagination.= "<a href=\"$targetpage?page=$next\">next</a>";
		else
			$pagination.= "<span class=\"disabled\">next</span>";
		$pagination.= "</div>\n";		
	}
?>

	<?php
		while($row = mysql_fetch_array($result))
		{
	
		echo ('<br>');
	echo '<div style="margin-left:52%;">';
	echo '<div style="  width:100%; . " id="Posts">';
	
	echo '<h3>' . $row["Title"] .'</h3>';
	echo 'posted by:' . $row["Name"] . "<br/>";
	echo 'Posted On:' . $row["Time"] . "<br/>";
	
	echo $row["postname"] . "<br/>";
	
	echo $row["Post"] . "<br/>";
	echo ('<a href=http://brightstardetailers.ddns.net/Barter/' . $row["postname"] . '.php' . '>Full Post</a>' . "<br/>");

	
	echo '</div>';
	echo '</div>';
	
		}
	?>

<?=$pagination?>

Upvotes: 0

Views: 1326

Answers (1)

Travesty3
Travesty3

Reputation: 14479

Your paginator loads up a new page. It does not forward the POST request on to the new page load, so when the page loads, $_POST is empty. There won't be a really great way of populating $_POST via a regular web link. You would have to either submit a form or do pagination via AJAX instead.

I think the easiest thing for you to do would be to skip using sessions for this, and instead wrap your pagination links in a <form>. Populate hidden inputs in this form with your $_POST data and use JavaScript to listen for a click event on each of the pagination links. When one of those links is clicked, either populate another hidden input with the page number (if you want to pass the page number in via POST), or modify the action attribute of the form to include the page number in the GET part of the URL. Then submit the form with JavaScript.

This would make it so that your POST data is populated after clicking the pagination link, in addition to the new page you want. No need to store stuff globally in the session.

Upvotes: 1

Related Questions