Custom Search Tool Optimization

6

Now, I'm creating a search page that receives, via POST , information from a form containing several checkbox . See below that there are several sequences of results and one orange is marked as an example:

WhenIclicksearch,theinformationissenttoapagewhereitwillcontainasequenceofifandcasetoorganizetheinformation:

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

AfterItreatthisinformation,Itreatitasaswitch:

switch($var1){case'comercial+mecanico+chave+segredo':$data=array('1'=>$_POST['chave'],'2'=>$_POST['segredo']);break;

Finally,Iuse$datatofiltermyloop:

$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,');

MYDOUBTS:

HowcanIoptimizethis?Everythingworksfine,butascheckboxeshavemanypossibilities,thecodeisgettingimmense!

Combinations:(commercial+residential+mechanical+electronic+key+secret+display+led)

(commercialand/orresidential+mechanicaland/orelectronic+keyand/orsecretand/ordisplayand/orled)

UPDATE:

  

Theproblemisthatthereisacombinationofcheck!Example:thereisthecommercialkeyandkeyforresidentialandthekeymayormaynotbemechanicalorelectric!HowdoIdothat?

Thereisahierarchybetweenthecheck:

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 
    
asked by anonymous 27.02.2015 / 21:29

2 answers

2

And if you do something like that does not help you?

<?php
function ohGogWhy($postData) {
    //query categories
    $categories = array(
        'comercial' => array(
            'eletronico', 
            'mecanico'
        ), 

        'residencial' => array(
            'eletronico', 
            'mecanico'
        )
    );

    $slug = array(
        'comercial' => 'co',
        'eletronico' => 'ele',
        'residencial' => 're',
        'mecanico' => 'mec'
    );

    $mainArr = isset($postData['main'])? $postData['main'] : array_keys($categories);
    $subArr = isset($postData['sub']) ? $postData['sub'] : $categories;

    $dataArr = array();

    //main
    if (isset($postData['subsub'])) {

        foreach($postData['subsub'] as $subsub) {

            foreach($mainArr as $main) {

                $tsub = (isset($subArr[$main])) ? $subArr[$main] : $subArr;

                foreach($tsub as $sub)
                    $data[] = $subsub . '-' . str_replace(array_keys($slug), array_values($slug), $main . '-' . $sub);
            }
        }
    }
    elseif (isset($postData['sub'])) {
        foreach($mainArr as $main) {

                $tsub = (isset($subArr[$main])) ? $subArr[$main] : $subArr;

                foreach($tsub as $sub)
                    $data[] = $sub . '-' . str_replace(array_keys($slug), array_values($slug), $main);
            }
    }
    else {
        $data = $mainArr;
    }

    return $data;
}

$data = array();
if (isset($_POST)) {
    $data = ohGogWhy($_POST);
    echo '<pre>'; print_r($data); echo '</pre>';
}

?>
<html>
    <title>lol</title>
    <body>
        <form method="POST">
            main:
            <input type="checkbox" name="main[]" value="comercial" multiple="false" />Comercial
            <input type="checkbox" name="main[]" value="residencial" multiple="false" />Residencial
            <hr>
            sub:
            <input type="checkbox" name="sub[]" value="mecanico" />Mecanico
            <input type="checkbox" name="sub[]" value="eletrico" />Eletronico
            <hr/>
            subsub:
            <input type="checkbox" name="subsub[]" value="chave" />Chave
            <input type="checkbox" name="subsub[]" value="segredo" />Segredo
            <input type="checkbox" name="subsub[]" value="display" />Display
            <input type="checkbox" name="subsub[]" value="led" />Led
            <hr>
            <input type="submit">
        </form>
    </body>
</html>

Validations to select only one element you do for JS

    
02.03.2015 / 13:34
0

To make it easier, you can put a vector of checkboxes in HTML where field value is the term to look for , so your form would look like this:

<input name="termos[]" type="checkbox" value="comercial">
<input name="termos[]" type="checkbox" value="residencial">
<input name="termos[]" type="checkbox" value="mecanico">
<input name="termos[]" type="checkbox" value="eletronico">
<input name="termos[]" type="checkbox" value="chave">
<input name="termos[]" type="checkbox" value="segredo">
<input name="termos[]" type="checkbox" value="display">
<input name="termos[]" type="checkbox" value="led">

When this form is received in PHP you will have a vector of $_POST['termos'] terms that can be entered directly into your search, thus dispensing with the treatment you are doing in the first part of the code:

$args = array(
    'posts_per_page' => -1,
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'product_cat',
            'field' => 'slug',
            'terms' => $_POST['termos']
        )
    ),
    'post_type' => 'product',
    'orderby' => 'title,'
);
    
01.03.2015 / 23:46