Dynamically assemble URL parameters

Asked

Viewed 786 times

4

I have a page in a system where I list the products registered in BD, I am implementing some filters within this page, and I would like to update the links of the filters as requested in the URL (GET REQUEST):

When starting the links the filters contained therein shall start as follows:

Category

<a href="meusite.com.br/produtos?_categoria=1">Categoria 1</a> 
<a href="meusite.com.br/produtos?_categoria=2">Categoria 2</a> 
<a href="meusite.com.br/produtos?_categoria=3">Categoria 3</a> 

Numbering

<a href="meusite.com.br/produtos?_numeracao=34">34</a>
<a href="meusite.com.br/produtos?_numeracao=35">35</a>
<a href="meusite.com.br/produtos?_numeracao=36">36</a>

If the user accesses the page filter the link`s shall update as follows:

If you access the filter category the links from the filter numbering should stay:

<a href="meusite.com.br/produtos?_categoria=1&_numeracao=34">34</a>
<a href="meusite.com.br/produtos?_categoria=1&_numeracao=35">35</a>
<a href="meusite.com.br/produtos?_categoria=1&_numeracao=36">36</a>

If you access the filter numbering the links filter category should stay:

<a href="meusite.com.br/produtos?_categoria=1&_numeracao=34">Categoria 1</a>
<a href="meusite.com.br/produtos?_categoria=2&_numeracao=34">Categoria 2</a>
<a href="meusite.com.br/produtos?_categoria=3&_numeracao=34">Categoria 3</a>

I would like a direction where to start, because I am not found the logic of how to do.

  • You want to get the values or keep them in the URL?

  • @Jorgeb.I want to keep in the URL take the values and insert in the filter link.

  • 2

    You asked a question thinking it’s the best way to create a filter for your products, but you don’t have the slightest idea of a logic for it. I advise you to show what you have and not just a part. And ask more concise questions.

2 answers

6


With the same principle as @Jorgeb’s answer, I adapted it to generate the filters specified in the question edit. Taking advantage, I added an option to remove an existing filter:

<?php
   $categoria = $_GET['_categoria'];
   $numeracao = $_GET['_numeracao'];

   $f_categoria = empty( $categoria ) ? '' :'&_categoria='.$categoria;
   $f_numeracao = empty( $numeracao ) ? '' :'&_numeracao='.$numeracao;

   if ( empty( $categoria ) ) {
      echo '<a href="meusite.com.br/produtos?_categoria=1'.$f_numeracao.'">Categoria 1</a>';
      echo '<a href="meusite.com.br/produtos?_categoria=2'.$f_numeracao.'">Categoria 2</a>';
      echo '<a href="meusite.com.br/produtos?_categoria=2'.$f_numeracao.'">Categoria 3</a>';
   } else {
      echo '<a href="meusite.com.br/produtos?_categoria='.$f_numeracao.'">Todas as categorias</a>';
   }

   if ( empty( $numeracao ) ) {
      echo '<a href="meusite.com.br/produtos?_numeracao=33'.$f_categoria.'">Numero 33</a>';
      echo '<a href="meusite.com.br/produtos?_numeracao=34'.$f_categoria.'">Numero 34</a>';
      echo '<a href="meusite.com.br/produtos?_numeracao=35'.$f_categoria.'">Numero 35</a>';
   } else {
      echo '<a href="meusite.com.br/produtos?_numeracao='.$f_categoria.'">Todas as numeracoes</a>';
   }
?>

If you are going to do something more complex, with many different filters, it pays to automate the whole process, using for example arrays and loops.

  • After all you understood the question the same way. I only answered a different question :)

  • 1

    @Jorgeb. Yeah, that’s why I think it’s wrong for -1 in yours. The way the question was, you did what you could.

  • 1

    Thank you all for your attention, as you can see I’m starting to use the OS, as I lost the logic posted what I had in mind to seek a direction and before my confused question I ended up finding this direction. Thanks again! @Bacco

  • 1

    @Vitormolina we are here for that. If you can, further detail the next questions, as it increases the chance of us giving good answers, and prevents colleagues from investing time based on wrong assumptions ;)

3

You can do it with a simple GET:

$categoria = $_GET['_categoria'];
$cor       = $_GET['_cor'];
$tamanho   = $_GET['_tamanho'];

then just check that the fields are filled in:

$url  = "Location: meusite.com.br/produtos.php?";

if(!empty($categoria)) 
    $url .= "_categoria=$categoria";

if(!empty($cor) && substr($url, -1)!="?")
    $url .= "&_cor=$cor";
else if(!empty($cor) && substr($url, -1)=="?")
    $url .= "_cor=$cor";

if(!empty($tamanho) && substr($url, -1)!="?")
    $url .= "&_tamanho=$tamanho";
else if(!empty($tamanho) && substr($url, -1)=="?")
    $url .= "_tamanho=$tamanho";

header('$url');

This answer is the answer to the first publication of the AP, anyway the idea is the same and for the last issue just refer to reply by @Bacco.

  • I’m having great difficulty in understanding what he wants @Bacco

  • 1

    I think I got it, I’m gonna risk an answer then.

  • @Bacco goes on and on :)

  • But I think the -1 they gave is exaggerated here, because the question could be better formulated.

  • @Bacco I answered as I realized, and I read it a few times. If you see my first answer you see what I’ve already thought about something else. It should have been closed, instead of trying to figure it out. It stays that way until the AP says something or your answer saves the question.

  • qq form upei your, because I think if there is problem, is not in the answer.

  • @Bacco I reread and still do not understand, would give the same answer lol

  • So I gave the negative, Jorge B. You still do not understand and answered without understanding.

  • 2

    @Lollipop What is strange, because what if the author likes his answer? Maybe you didn’t understand and Jorgeb did. I find it difficult to vote if you do not understand. By the way, it is one of the biggest problems of Sopt.

  • There’s nothing strange and you know very well that’s not it.

  • Yeah, "weird" is a polite way of saying.

  • 2

    @Bacco I understood the question, of my form and answered. Let’s see what says the AP if appear...

  • My keyboard is bad. Sorry.

  • 2

    @Lollipop In the ideal world no one should answer a confusing question, but IF does not want it so, then everyone is answering what they interpret correctly and takes the inherent risks of it. But just as it’s not possible to be positive without knowing if it’s right, you can’t be negative without knowing if it’s wrong. To vote positive or negative you have to make sure that the vote is appropriate. It cannot be an assumption, even because the person forgets to change afterwards. Who answers does not forget because people will interact with the answer, with the vote this does not happen.

  • @Mustache should be closed

  • I’m enlightened @bigown. I just did what they did to me and learned, but now I’ll replace it with this learning. Can you dictate the @Jorge answer? Let’s forget this whole mess!

  • 1

    @Lollipop but if you’re wrong, if the person can’t fix it (warning the person to fix a problem is priority), then it’s negative. Things you are sure are wrong and will not be fixed deserve negative to help sort the content, separate the tares from the wheat. I’m sorry that someone wrongly quoted something from you. But you also need to see if the person had no conviction that you were wrong. Anyway she should tell you to clean up.

  • @Lollipop I edited, but I found it unnecessary to change the answer. I added an explanation.

Show 13 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.