I have two requests to my server. The first one gets categories and the output to URL is something like this: doomain.com?category=1. The second request is meant for sorting the data in that specific category. This request outputs something like: domain.com?sort-by=name. What I need is something like this: domain.com?category=1&sort-by=name. I cant do this at the moment because whenever I submit a form it starts a new request. I need to remember the previous request and then add a new request to it with on "&" sign. Or if there is no previous request then do it normally with "?" sign. How can I achieve such thing?

4 Years
Discussion Span
Last Post by diafol

How do you now output that link? Can you show some code?

I'd use an url building method like this:

$vars = array ();
$vars['sort-by'] = 'name';
$vars['category'] = 1;
$url = 'domain.com?' . implode('&', $vars);

I have seperate drop down menu for sorting:

<div id="sort-box">
    <form action="?" method="get" name="sorter">
    <p>Sorteeri: </p>
        <select name="sort-by" onchange="this.form.submit()" >
            <option value="nimi_ASC">Nimi(kasvav)</option>
            <option value="nimi_DESC">Nimi(kahanev)</option>
            <option value="hind_DESC">Hind(kahanev)</option>
            <option value="hind_ASC">Hind(kasvav)</option>
            <option value="lisatud_ASC">Vanemad eespool</option>
            <option value="lisatud_DESC">Uuemad eespool</option>

And then I have list of categories to choose from:

<ul id="category-list" class="list-horizontal">
    <li class="first-left" ><a href="?feature-cat=-1">Kõik</a><li>
        foreach(get_category_list() as $kategooria){
            echo "<li><a href='?feature-cat=$kategooria->ID'>$kategooria->nimi</a></li>";

This basically just generates a list of categories from database and then when I click on a category it outputs products in that specific category.



How do I use your method with forms ? With many forms ? Please elaborate.

I dont think I can use your method, because your method would leave url domain.com?name&1. I have nothing to do with those. Could it be that your method needs url rewrite with .htaccess. If so, then its not really suitable because I dont know much about htaccess.

Edited by Martin C++


The method just shows you how to build those URL's, and no, it doesn't require url rewriting. Your right, the implode is not the right way. It should look like this:

$vars = array ();
$vars['sort-by'] = 'name';
$vars['category'] = 1;

$parts = array ();
foreach ($vars as $key => $value)
    $parts[] = $key . '=' . $value;

$url = 'domain.com?' . implode('&', $parts);

Edited by pritaeas


I am sorry, but I still dont get this. I mean, how do I set those variables? If I make a request then all the parts should be in the $_REQUEST[]. I cant just set arrays. I have to set $_REQUEST['get_page'], $_REQUEST['feature-cat'] and $_REQUEST['sort-by'] all at the same time. BUT they dont have to be set on the same time. If the users has not used sorting then sort0by would not be set. I mean I could make tens of IF statements but thats just brutal and impractical. I almost get your method but I need to know how do you set the array keys. You would still have to make a request to set them, dont you ? And if you do then you already gave an awway of variables called $_REQUEST[] ?


If I understand you, you need to click a link to get the category, but you also need to take the value in the dropdown to enable sorting.

I'd probably use js to submit the values.

If you provide a class for each link, e.g.

<a href='?feature-cat=$kategooria->ID' class = 'cat'>$kategooria->nimi</a>

You can then add the sort method to the url. Using jquery, something like this?

    var href = $(this).attr('href'); 
    $(this).attr('href', href + '&sort-by=' + $('#sort-by').val());
    return true;

Your form just needs a dropdown - no need for actions/methods or submit buttons.
Not able to test at the moment.


Yes, that is exactly what I need. But will it still work when the category is not set ? That is when the user hasn't specified what category he wants to see so all the products in the category will be shown instead. That would mean the featrue-cat is not set and therefor the sort-by part can not be with & but with ?.


I do have dropdown menu and it submits itself when it's value is changed. And another thing. Another quick question. Is there some some approved method that is being used to solve these kind of problems. I mean big CMS has to have some way to handle this situations. Maybe there is some kind of a term that I can google for and learn more about it. I feel like this a very important part of web developement and I need to master it.

Thank you for your reply, it was already very helpful.


The category is not set on page load I take it. So in that case all are shown. You could set category_id to 0 for the show all and then pick up 0 in your $_GET so that it modifies the SQL not to include a WHERE clause. There are a million ways to skin a cat.

You could set up some javascript to deal with page load, building urls on the fly.
You could choose a category from a dropdown - that way all your data could be in $_POST (or $_GET, I suppose) without any need for js.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.