Optimization of custom search tool

Asked

Viewed 256 times

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:

inserir a descrição da imagem aqui

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:

inserir a descrição da imagem aqui

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?

  • It is an example for the case of the image above, because realize that chave and segredo are selected!

  • 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. .

  • 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 sum comercial + 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 Would it be something like this: http://pastebin.com/6kB56ksg ? I think I understand +- what you want to do..

2 answers

2


And if you do something like that it doesn’t 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>

The validations to select only one element you do by JS

0

For ease you can mount a vector of checkboxes in HTML where the field value is the term to be searched for, so your form would look that way:

<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 an array of terms $_POST['termos'] which can be entered directly into your search and thus dispenses 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,'
);

Browser other questions tagged

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