Pagination with multiple queries

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mstrnicegui
Forum Newbie
Posts: 3
Joined: Fri Jun 17, 2005 3:01 pm

Pagination with multiple queries

Post by mstrnicegui »

I am a new php coder, trying to set up my first site with pagination. I used the code posted by nuke at webhostingtalk.com forums (second to last post on the page). I was able to get pagination to work fine, but ran into trouble with the code used to parse the query string. I am trying to break up a list into multpile pages with multiple ways to be ordered.

Code: Select all

if ( empty($_SERVERї&quote;QUERY_STRING&quote;]) )
{
  $_SERVERї&quote;QUERY_STRING&quote;] = &quote;?&quote;;
} else {
  $_SERVERї&quote;QUERY_STRING&quote;] = $_SERVERї&quote;QUERY_STRING&quote;] . &quote;&&quote;;

}
When used in conjunction with this code to build the links, it produces undesired results.

Code: Select all

echo &quote;<a href=\&quote;{$_SERVERї&quote;PHP_SELF&quote;}{$_SERVERї&quote;QUERY_STRING&quote;}page=$prev\&quote;><< Prev</a> &quote;;

Code: Select all

http://127.0.0.1/taquerias.php?page=2&page=1&page=3&page=4
Every time you click on a new page, it adds a new query to the url, instead of replacing the old one. I've tried some string parsing to remove the old query before it adds the new one, but it doesn't enter the if($pos) statement.

Code: Select all

if ( empty($_SERVERї&quote;QUERY_STRING&quote;]) )
    {
        $_SERVERї&quote;QUERY_STRING&quote;] = &quote;?&quote;;
    }
    else
    {
		$findme = 'page';
		$pos = strrchr($_SERVERї'QUERY_STRING'], $findme);

		if ($pos === TRUE) {
			$_SERVERї&quote;QUERY_STRING&quote;] = substr_replace($_SERVERї'QUERY_STRING'], '', -6);
		}
		$_SERVERї&quote;QUERY_STRING&quote;] = &quote;?&quote;.  $_SERVERї&quote;QUERY_STRING&quote;] . &quote;&&quote;;
    }
I would simply remove the query string portion of the code, but I also have queries going to order the list in multiple ways which would be lost if I didn't include the query string.

Is there an easier way for me to avoid getting results like this?

Code: Select all

http://127.0.0.1/taquerias.php?page=2&page=1&page=3&page=4
User avatar
ambivalent
Forum Contributor
Posts: 173
Joined: Thu Apr 14, 2005 8:58 pm
Location: Toronto, ON

Post by ambivalent »

I'm not so much an expert to comment on the code you posted, but I previously found a tutorial on pagination that I easily adapted to fit in with my existing code:

http://www.phpfreaks.com/tutorials/43/0.php

There seemed to be a couple of minor errors with it, such as (from page 5)

Code: Select all

echo("<a href=\"$PHP_SELF&page=$i\">$i</a> ");
vs. what actually worked

Code: Select all

echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");
note the "?" replaced the "&".

Also from page 5

Code: Select all

$pagenext   = $page++;
didn't seem to work as advertised and was replaced with:

Code: Select all

$pagenext = $page+1;
You may find this works better with what you have. Don't forget to check the discussion and comments at the end as there were a couple of revisions.

HTH
mstrnicegui
Forum Newbie
Posts: 3
Joined: Fri Jun 17, 2005 3:01 pm

Post by mstrnicegui »

Thank you for the response, but I guess I wasn't clear where my question lies. I have the pagination working as expected, but I do not have the code parsing so that the url doesn't keep increasing in length as you keep clicking the links for the different pages.

When clicking on the page links to skip to the next page, the url does the following:

Code: Select all

http://127.0.0.1/pages.php?page=1
http://127.0.0.1/pages.php?page=1&page=2
http://127.0.0.1/pages.php?page=1&page=2&page=3
http://127.0.0.1/pages.php?page=1&page=2&page=3&page=4
I want it to come out like:

Code: Select all

http://127.0.0.1/pages.php?page=1
http://127.0.0.1/pages.php?page=2
http://127.0.0.1/pages.php?page=3
http://127.0.0.1/pages.php?page=4
User avatar
ambivalent
Forum Contributor
Posts: 173
Joined: Thu Apr 14, 2005 8:58 pm
Location: Toronto, ON

Post by ambivalent »

I know what you meant, I was suggesting a different route.

Near as I can tell, the problem comes here:

Code: Select all

echo "<a href=\"{$_SERVER["PHP_SELF"}{$_SERVER["QUERY_STRING"}page=$prev\"><< Prev</a>&nbsp;";
QUERY_STRING "GETS" the query passed to it from the "$whateverPage" and then appends the additional query: "$prev". Each successive click adds to to it.

Someone will correct me (please) if my assessment above is wrong, but, as I see it, you may be better off with the alternative suggested.
mstrnicegui
Forum Newbie
Posts: 3
Joined: Fri Jun 17, 2005 3:01 pm

Post by mstrnicegui »

I have narrowed the problem down to:

Code: Select all

if ( empty($_SERVER["QUERY_STRING"]) )
    {
        $_SERVER["QUERY_STRING"] = "?";
    }
    else
    {
        $findme = 'page';
        $pos = strrchr($_SERVER['QUERY_STRING'], $findme);
 
        if ($pos === TRUE) {
            $_SERVER["QUERY_STRING"] = substr_replace($_SERVER['QUERY_STRING'], '', -6);
        }
        $_SERVER["QUERY_STRING"] = "?".  $_SERVER["QUERY_STRING"] . "&";
    }
and

Code: Select all

echo "<a href=\"{$_SERVER["PHP_SELF"}{$_SERVER["QUERY_STRING"}page=$prev\"><< Prev</a>&nbsp;";

The problem is that in the second portion of the if statment, it doesn't parse the query string to see if it includes a page=x query. It then goes into the second quoted php code (this is just the example, it happens with previous, any page number, and next) and adds another query for page=x, even if one exists. I need to find a way to destroy an existing page=x query from the query string without destroying the query string (since I might have a query trying to order the list by 1 of 5 ways). The page=x query will be the last query to appear on the list.

I have put in code that I think works for parsing the query, but it doesn't work. Is there a way I can parse the query string to delete the page=x query, or is there a way I can increment the page=x query in the query string without trashing any other queries that might be in the string?
Post Reply