Jump to content

[SOLVED] simple pagination


wrathican

Recommended Posts

hey guys

 

i have an image gallery and im using pagination to to split it up into chunks.

its a very simple pagination and it works perfectly fine.

 

the only problem i have is that im using some GET vars to control what i get form the database and when browsing through multiple pages my url starts to look like this:

/gallery.php?func=viewal&alid=4&page=2&page=1&page=2

 

the bold bits being the problem

 

im using the REQUEST_URI part of the $_SERVER array. any ideas how i could fix this?

 

 

code (im using htmlspecialchars so that the page is standards compliant):

<?php
	$server = htmlspecialchars($_SERVER['REQUEST_URI']);

	if ($page == 1) {

  		echo " First Prev ";

	} else {

	   echo " <a href='".$server."&page=1'>First</a> ";
	   $prevpage = $page-1;
	   echo " <a href='".$server."&page=$prevpage'>Prev</a> ";
	}

	echo " ( Page $page of $lastPage ) ";

	if ($page == $lastPage) {

		echo " Next Last ";

	} else {
		$nextpage = $page+1;
		echo " <a href='".$server."&page=$nextpage'>Next</a> ";
		echo " <a href='".$server."&page=$lastPage'>Last</a> ";
	}
	?>

 

Link to comment
https://forums.phpfreaks.com/topic/119022-solved-simple-pagination/
Share on other sites

Problem is with the use of REQUEST_URI as it includes the query string. So every time you click a link it is always going to add &page=X to your url.

 

What I'd do is dynamically build the url. Like so

$server = $_SERVER["SCRIPT_NAME"];

// check if the page variable exits
if(isset($_GET['page']))
{
    $page = $_GET['page'];

    // remove the page from the $_GET array
    unset($_GET['page']);
}

// no we'll dynamically build the query string
$server .= '?'. http_build_query($_GET);

echo $server;

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.