Hello, Im trying to get the following working:

I have a list, loaded from a MySQL database, and I have links to sort the list by name, date, id, etc.

The structure to determine if its going to sort everything ASC or DESC is something like

if ($_GET['order'] == "ASC") $order = "DESC";
  else $order = "DESC";
$sort = $_GET['sort'];

//here, $order could be either ASC or DESC
//and $sort could be 'name', 'date', 'id', etc.

then the links look like this

//for this example,
//$sort = "name";
//$order = "ASC";

echo '<a href="?sort='.$sort.'&order='.$order.'">$sort</a>';

so anyway, when you click on that, the new address goes from, lets say,
http://localhost/path/to/file/index.php
to
http://localhost/path/to/file/index.php?sort=name&order=ASC

Everything 'good' so far.
The thing now is, I added a variable to know wether the list should display all the items in it or just some of them and page to display the rest.

so I have a link like this:
<a href="?show=all">Show All</a>

The address used to look like this:
http://localhost/path/to/file/index.php?sort=name&order=ASC

and now it is [after you click "Show All"]
http://localhost/path/to/file/index.php?show=all

I know the code I have is working, its just doing what its supposed to, but what I want is to get this:
http://localhost/path/to/file/index.php?sort=name&order=ASC&show=all

in other words, get everything after '.php?' and add '&show=all'
and the same for the sorting; if I have '.php?show=all', add '&sort=$sort&order=$order'

So bleh thats about it, thanks in advance for any help,
-javier

    You can use foreach on $GET:

    $url = 'index.php?';
    foreach ($_GET as $key=>$value)
              $url.=$key.'='.urlencode($value).'&';
    $url.='show=all';
    

      everything after the .php is the querystring.. So you could get that with :

      $query = $_SERVER['QUERY_STRING']
      If(!$query) {
           $query .= "&show='all'";
      } else {
           $query = "?show='all'";
      }
      

      This will create just show='all' if no query exists. If it does, it will just add the &show='all' after it.

      Then, use your link as :

      <a href="<?php echo $query; ?>l">Show All</a>

        Thanks! I'll try that when I get home and tell you how it works.

          While you're there, have a look at [man]http_build_query[/man] as well. That might reduce some of the work.

            I tried that code and it kind of worked,

            $query = $_SERVER['QUERY_STRING'];
            
            if($query) $trailQuery = "?".$query."&";
              else $trailQuery = "?";
            

            Lets say I have the following links
            NAME [?sort=name&order=ASC]
            DATE [?sort=date&order=ASC]
            STATUS [?sort=status&order=ASC]

            PAGE1 [?page=1]
            PAGE2 [?page=2]
            PAGE3 [?page=3]

            and I want to be able to keep the query string when I'm browsing all the pages, so if I have
            ?sort=name&order=DESC
            and I click on page 2, I want it to look like
            ?sort=name&order=DESC&page=2

            then, if I click on page 3, I want it to only change the page number like this:
            ?sort=name&order=DESC&page=3
            instead, I get this
            ?sort=name&order=DESC&page=2&page=3

            same for the sorting, if I sort the list by name, ASC, I get
            ?sort=name&order=ASC
            then if I click again to sort it by name, but DESC this time, I get
            ?sort=name&order=ASC&sort=name&order=DESC

            is there anyway to go through the current querystring to avoid repeating the variable name and just change the value, keeping the names/values that dont change?

            Im not sure if I made myself clear, ask if you have any questions regarding the explanation, and again, thanks in advance for any help.

              That it gets ordered DESC is in your code:

              if ($_GET['order'] == "ASC") $order = "DESC";
                else $order = "DESC";
              

              Since it does the same thing no matter what value you have in the get value order you could change it to

              $order = "DESC";
              

              I don't think that is what you intended, so the best way is to solve the problem with your code.

              About the problem with the query string I recommend that you go through the variables on at a time and not include the page variable in the new string.

                The real code isn't wrong, the example I posted is different from what I really have, it looks more like

                if ($_GET['order'] == "ASC") $order = "DESC";
                  else $order = "ASC"; 
                

                so if the order is DESC or there's no order at all, it is going to be ASC, else order = DESC.

                Anyway, that's not the problem, the problem is with the query string, I want to be able to add a variable that doesnt exist to it, for example if I have
                ?order=NAME&sort=ASC
                I want to be able to add a non-existing variable, such as 'page'
                ?order=NAME&sort=ASC&page=2

                BUT if a variable already exists in the query string, I want to update the value
                lets say I press a link to browse through page 3, and the current query string is
                ?order=NAME&sort=ASC&page=2
                I want the new query string to only update the page value, like this
                ?order=NAME&sort=ASC&page=3
                insted, I get this
                ?order=NAME&sort=ASC&page=2&page=3
                so if I keep browsing through all the pages, I get like
                ?order=NAME&sort=ASC&page=2&page=3&page=4
                ?order=NAME&sort=ASC&page=2&page=3&page=4&page=5
                ?order=NAME&sort=ASC&page=2&page=3&page=4&page=5&page=6

                it works, I get to see page 5, 6, 7, etc., but I'll end up with a 324938492 characters query string this way.

                imagine if I keep sorting the list in a different way, I'd get something like
                ?order=NAME&sort=ASC&page=2&page=3&page=4&page=5&page=6&order=DATE&sort=ASC
                and if I keep clicking
                ?order=NAME&sort=ASC&page=2&page=3&page=4&page=5&page=6&order=DATE&sort=ASC&order=DATE&sort=DESC

                get it now? I end up with a really big string, I want to only UPDATE the current query variables, instead of adding em to the end of the string if they already exist.

                Bleh I hope I made my point this time, thanks again for any help.

                  What you could do use [man]parse_str[/man] to get all the bits into an array, work with the array (adding, removing and updating values as you go), and then [man]http_build_query[/man] to reassemble the new query string from the array.

                  parse_str($querystring, $queryparts);
                  $queryparts['page']=5;
                  $querystring = http_build_query($queryparts);

                    Thank you for the reply.

                    I got it solved now, what I did [in case anyone has this problem] was something like this;

                    $querystring = '';
                    $add = '';
                    
                    if(isset($HTTP_GET_VARS['sort']))
                    {
                      $sort = ($HTTP_GET_VARS['sort'] != '') ? $HTTP_GET_VARS['sort'] : 'date';
                      $querystring .= ''.$add.' sort='.$sort.'';
                    
                      $add = '&';
                    }
                    if(isset($HTTP_GET_VARS['order']))
                    {
                      $order= ($HTTP_GET_VARS['order'] != 'ASC') ? 'ASC' : 'DESC';
                      $querystring .= ''.$add.' order='.$order.'';
                    
                      $add = '&';
                    }
                    

                    and basically I kept going on like that to get all the current values and make the changes where they needed to be made.

                    Might be kind of messy but that was my solution to this problem.
                    If anyone knows of another way, let me know.

                    -javier

                      javier_st wrote:

                      If anyone knows of another way, let me know.

                      thump

                        Weedpacket wrote:

                        thump

                        Besides yours, of course.

                        //Edit
                        By the way, the reason why I didnt use your method was because I already had done mine, and since Im using this for links, I'd have to update the values for every link, which I did. As I said, the code I came up with is kind of messy.

                        in each condition I ended up with something like

                        for($j; $j<=4; $j++){
                        $querystring[$n] .= 'order='.$arrOrder[$n].'';
                        }
                        

                        What I meant by 'another way' was a cleaner way.
                        Still thinking on how to get something easier-to-edit, since this is going to get way messier when I need to work with more variables.

                          Write a Reply...