Jump to content

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;

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.