Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Senior Coder
    Join Date
    Oct 2003
    Location
    Australia
    Posts
    1,963
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Altering Pagination generated query string

    Hey
    I've using a *cough* DW generated pagination script (i know, blasphemy - whatever ) 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:
    PHP Code:
    $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
    Many thanks

    I take no responsibility for the above nonsense.


    Left Justified

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Not quite sure if that's what you're after...

    PHP Code:
    $queryString_qLinks sprintf("%s&totalRows_qLinks=%d"$queryString_qLinks$totalRows_qLinks); 
    De gustibus non est disputandum.

  • #3
    Senior Coder
    Join Date
    Oct 2003
    Location
    Australia
    Posts
    1,963
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Almost...

    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

    I take no responsibility for the above nonsense.


    Left Justified

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    De gustibus non est disputandum.

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mindlessLemming
    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.
    De gustibus non est disputandum.

  • #6
    Senior Coder
    Join Date
    Oct 2003
    Location
    Australia
    Posts
    1,963
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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..)
    PHP Code:
    $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 Code:
    <?php if ($pageNum_qLinks 0) { // Show if not first page ?>
    <a href="<?php printf("%s?pageNum_qLinks=%d%s"$currentPagemax(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"$currentPagemin($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

    I take no responsibility for the above nonsense.


    Left Justified

  • #7
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    A quick hack, without testing it, but it looks like the same pattern repeated. Change

    PHP Code:
    printf("%s?pageNum_qLinks=%d%s"$currentPagemax(0$pageNum_qLinks 1), $queryString_qLinks); 
    to

    PHP Code:
    printf("%s?pageNum_qLinks=%s%d"$currentPage$queryString_qLinksmax(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

    PHP Code:
    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...
    De gustibus non est disputandum.

  • #8
    Senior Coder
    Join Date
    Oct 2003
    Location
    Australia
    Posts
    1,963
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 Cheers!

    I take no responsibility for the above nonsense.


    Left Justified

  • #9
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

    PHP Code:
    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 Code:
    <?php if ($pageNum_qLinks 0) { // Show if not first page ?>
    <a href="<?php printf("%s?%s&pageNum_qLinks=%d"$currentPage$queryString_qLinksmax(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_qLinksmin($totalPages_qLinks$pageNum_qLinks 1)); ?>" title="Next page">Next</a>
    <?php // Show if not last page ?>
    Does that help?
    De gustibus non est disputandum.

  • #10
    Senior Coder
    Join Date
    Oct 2003
    Location
    Australia
    Posts
    1,963
    Thanks
    0
    Thanked 0 Times in 0 Posts
    "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

    I take no responsibility for the above nonsense.


    Left Justified

  • #11
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    LOL. You're welcome.
    De gustibus non est disputandum.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •