...

View Full Version : Altering Pagination generated query string



mindlessLemming
07-09-2004, 08:33 AM
Hey :)
I've using a *cough* DW generated pagination script (i know, blasphemy - whatever :rolleyes: ) and I'd like to change the way it generates the query string so that I can use mod_rewrite to ditch the query string entirely.
The important bit is posted below:


$queryString_qLinks = "";
if (!empty($_SERVER['QUERY_STRING'])) {
$params = explode("&", $_SERVER['QUERY_STRING']);
$newParams = array();
foreach ($params as $param) {
if (stristr($param, "pageNum_qLinks") == false &&
stristr($param, "totalRows_qLinks") == false) {
array_push($newParams, $param);
}
}
if (count($newParams) != 0) {
$queryString_qLinks = "&" . htmlentities(implode("&", $newParams));
}
}
$queryString_qLinks = sprintf("&totalRows_qLinks=%d%s", $totalRows_qLinks, $queryString_qLinks);

The problem is that I'm already passing a variable via the URI and the pagination script causes my variable to come last, whereas usually it comes first.
According to my very small knowledge of mod_rewrite, don't the variables always have to be in the same order for it to work properly?
What I'd ideally like is if someone could show me what I need to change so as to make my variable always come first...
I'm pretty lost on this one, as I odn't really know what I'm looking at :o
Many thanks ;)

mordred
07-09-2004, 08:55 AM
Not quite sure if that's what you're after...



$queryString_qLinks = sprintf("%s&totalRows_qLinks=%d", $queryString_qLinks, $totalRows_qLinks);

mindlessLemming
07-09-2004, 09:30 AM
Thanks for that Ben, but now it's in the middle instead of last. I' just need to shift it one more to the left...
Can you tell me what the change you made actually means please?

("%s&totalRows_qLinks=%d", $queryString_qLinks, $totalRows_qLinks);

Thanks mate :thumbsup:

mordred
07-09-2004, 11:23 AM
I hacked with the code you posted initially, and called the file with the query string "foo=bar". With my modification, it always comes out at the most left position, but with an ampersand prefixed, which indicates that something's going to be prepended to it... but I don't know, it's not in the code. Post the whole thing so I can see where the rest of the $querySTring_qLinks is generated.

mordred
07-09-2004, 11:27 AM
Can you tell me what the change you made actually means please?

("%s&totalRows_qLinks=%d", $queryString_qLinks, $totalRows_qLinks);


Yes, sprintf() takes a format string (a string with some format identifiers in it) and a variable number of further arguments. Each argument that follows is inserted into the format string where the next identifier is, e.g. first argument goes into first identifier. An identifier is everything starting with %. '%s' means to convert the variable to a string, '%d' means to convert it to a decimal number. This type of formatting is a legacy of the C language, FWIW.

What I did change was just the position where $queryString_links should be inserted, I put it at the beginning and not at the end. Of course the order of arguments had to be changed as well to accomodate this modification.

mindlessLemming
07-09-2004, 11:36 AM
:( Even when you try to explain it in simple terms it goes straight over my head...
Ok, here's the entire php block (including useless SQL queries..)


$currentPage = $_SERVER["PHP_SELF"];

$maxRows_qLinks = 6;
$pageNum_qLinks = 0;
if (isset($_GET['pageNum_qLinks'])) {
$pageNum_qLinks = $_GET['pageNum_qLinks'];
}
$startRow_qLinks = $pageNum_qLinks * $maxRows_qLinks;

mysql_select_db($database_mindless, $mindless);
$query_qLinks = "SELECT * FROM tlinks ORDER BY date_post DESC";
$query_limit_qLinks = sprintf("%s LIMIT %d, %d", $query_qLinks, $startRow_qLinks, $maxRows_qLinks);
$qLinks = mysql_query($query_limit_qLinks, $mindless) or die(mysql_error());
$row_qLinks = mysql_fetch_assoc($qLinks);

if (isset($_GET['totalRows_qLinks'])) {
$totalRows_qLinks = $_GET['totalRows_qLinks'];
} else {
$all_qLinks = mysql_query($query_qLinks);
$totalRows_qLinks = mysql_num_rows($all_qLinks);
}
$totalPages_qLinks = ceil($totalRows_qLinks/$maxRows_qLinks)-1;

$colname_qSearch = "css";
if (isset($_POST['searchTerm'])) {
$colname_qSearch = (get_magic_quotes_gpc()) ? $_POST['searchTerm'] : addslashes($_POST['searchTerm']);
}
mysql_select_db($database_mindless, $mindless);
$query_qSearch = sprintf("SELECT * FROM tlinks WHERE (title LIKE '%%%s%%' OR tlinks.desc LIKE '%%%s%%')", $colname_qSearch,$colname_qSearch);
$qSearch = mysql_query($query_qSearch, $mindless) or die(mysql_error());
$row_qSearch = mysql_fetch_assoc($qSearch);
$totalRows_qSearch = mysql_num_rows($qSearch);

$queryString_qLinks = "";
if (!empty($_SERVER['QUERY_STRING'])) {
$params = explode("&", $_SERVER['QUERY_STRING']);
$newParams = array();
foreach ($params as $param) {
if (stristr($param, "pageNum_qLinks") == false &&
stristr($param, "totalRows_qLinks") == false) {
array_push($newParams, $param);
}
}
if (count($newParams) != 0) {
$queryString_qLinks = "&" . htmlentities(implode("&", $newParams));
}
}
$queryString_qLinks = sprintf("&totalRows_qLinks=%d%s", $totalRows_qLinks, $queryString_qLinks);


And here's where it's inserted into the page...


<?php if ($pageNum_qLinks > 0) { // Show if not first page ?>
<a href="<?php printf("%s?pageNum_qLinks=%d%s", $currentPage, max(0, $pageNum_qLinks - 1), $queryString_qLinks); ?>" title="Previous page"><img src="images/arrow_prev.gif" alt="Previous page" />Previous</a>
<?php } // Show if not first page ?>
<?php if ($pageNum_qLinks < $totalPages_qLinks) { // Show if not last page ?>
<a href="<?php printf("%s?pageNum_qLinks=%d%s", $currentPage, min($totalPages_qLinks, $pageNum_qLinks + 1), $queryString_qLinks); ?>" title="Next page"><img src="images/arrow_next.gif" alt="Next page" />Next</a>
<?php } // Show if not last page ?>


I know it's probably very bloated code, but one step at a time, eh? ;)
You rock Ben, thanks for helping me out :thumbsup:

mordred
07-09-2004, 11:59 AM
A quick hack, without testing it, but it looks like the same pattern repeated. Change



printf("%s?pageNum_qLinks=%d%s", $currentPage, max(0, $pageNum_qLinks - 1), $queryString_qLinks);


to



printf("%s?pageNum_qLinks=%s%d", $currentPage, $queryString_qLinks, max(0, $pageNum_qLinks - 1));


for the "previous" link. You have to put my former modification into the code too. See if that works, if not I have a closer look at it. Regarding my attempt to explain how sprintf() works... maybe you could hack a little with a code like



print sprintf("hello %s world", "mindless");
print sprintf("hello world %s", "mindless");
print sprintf("hello %d world", "23.5");


to get a feeling how it's supposed to work. Oh, I'm supposed to be working too right now, hehe... ;)

mindlessLemming
07-09-2004, 12:46 PM
Unfortunately that b0rks it in a big way if I do the same for the 'Next' link (changing min to max, etc) :(

The code for 'Previous' still leaves the cat variable in the middle.
Dang...

Oh, and that little example made things so much clearer for me :D Cheers!

mordred
07-09-2004, 02:20 PM
Argh, where was my mind? The code I posted is faulty. I had a closer look at the complete thing and I got it working now, the changes are:



if (!empty($_SERVER['QUERY_STRING'])) {
$params = explode("&", $_SERVER['QUERY_STRING']);
$newParams = array();
foreach ($params as $param) {
if (stristr($param, "pageNum_qLinks") == false &&
stristr($param, "totalRows_qLinks") == false) {
array_push($newParams, $param);
}
}
if (count($newParams) != 0) {
$queryString_qLinks = htmlentities(implode("&", $newParams));
}
}
$queryString_qLinks = sprintf("%s&totalRows_qLinks=%d", $queryString_qLinks, $totalRows_qLinks);



and



<?php if ($pageNum_qLinks > 0) { // Show if not first page ?>
<a href="<?php printf("%s?%s&pageNum_qLinks=%d", $currentPage, $queryString_qLinks, max(0, $pageNum_qLinks - 1)); ?>" title="Previous page">Previous</a>
<?php } // Show if not first page ?>
<?php if ($pageNum_qLinks < $totalPages_qLinks) { // Show if not last page ?>
<a href="<?php printf("%s?%s&pageNum_qLinks=%d", $currentPage, $queryString_qLinks, min($totalPages_qLinks, $pageNum_qLinks + 1)); ?>" title="Next page">Next</a>
<?php } // Show if not last page ?>


Does that help?

mindlessLemming
07-09-2004, 02:59 PM
"Does that help?"
Are you kidding?! I'm going to name my first born after you - that's how much it helped!
You're a kind and patient man, Ben.
Thanks again :D

mordred
07-09-2004, 03:06 PM
LOL. You're welcome. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum