6
Guys, I’m creating a search page that gets, via POST
, the information on a form containing several checkbox
. See below that there are several sequences of results and is marked in orange one of them as an example:
When I click on seek out, the information is sent to a page where it will contain a sequence of if
and case
to organize the information:
if(!isset($_POST['residencial']) &&
isset($_POST['comercial']) &&
isset($_POST['mecanico']) &&
!isset($_POST['eletronico']) &&
isset($_POST['chave']) &&
isset($_POST['segredo']) &&
!isset($_POST['display']) &&
!isset($_POST['led'])){
$var1 = $_POST['residencial'].'+'.
$_POST['mecanico'].'+'.
$_POST['display'];
}
echo $var1;
RESULT: commercial + mechanical + key + secret
After I treat this information, I treat it in a switch
:
switch ($var1){
case 'comercial + mecanico + chave + segredo ':
$data = array(
'1' => $_POST['chave'],
'2' => $_POST['segredo']
);
break;
Finally, I use the $data
to filter my loop:
$args = array(
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
// 'terms' => 'white-wines'
'terms' => array(
$data['1'],
$data['2']
)
)
),
'post_type' => 'product',
'orderby' => 'title,'
);
MY DOUBT:
How can I optimize this? Everything works fine, but as the checkbox has many possibilities, the code is getting immense!
Combinations: (commercial+residential+mechanical+electronic+key+secret+display+led)
(commercial and/or residential + mechanical and/or electronic + key and/or secret and/or display and/or led)
UPDATE:
The problem is that there is a combination check! Example: there is the key for commercial and key for residential and besides the key may or may not be mechanical or electric! As I do in this case?
There is a hierarchy between the check:
Comercial(categoria)
-Mecânico(sub categoria)
--Chave(sub)
--Segredo(sub)
--Display(sub)
--Led (sub)
-Eletrônico(sub categoria)
--Chave(sub)
--Segredo(sub)
--Display(sub)
--Led (sub)
Residencial(categoria)
-Mecânico
--Chave
--Segredo
--Display
--Led
-Eletrônico
--Chave
--Segredo
--Display
--Led
Why when this happens:
case 'comercial + mecanico + chave + segredo ':
 $data = array(
 '1' => $_POST['chave'],
 '2' => $_POST['segredo']
 );
break;
you only play two posts in the array?– Gabriel Tadra Mainginski
It is an example for the case of the image above, because realize that
chave
andsegredo
are selected!– Lollipop
Look, you may not have considered what Dudu said, but of those ifs he already saved you, what I suggest is to use array_push() for that data inside the switch. .
– Gabriel Tadra Mainginski
The problem is that you are not understanding that I am using the resquest post to form a value for
$var1
. I just want to know how I make the sumcomercial + mecanico + chave + segredo
, without using many codes. This sum will determine there in the switch ($var1). How do I know who’s who when you have a subcategory? Check out the update I did! Of course.– Lollipop
@Lollipop Would it be something like this: http://pastebin.com/6kB56ksg ? I think I understand +- what you want to do..
– stderr