How to use user informed search conditions (not required)

1

My question is a little complicated to explain, but come on. I have several checkboxes, where it is not mandatory to select them to do the search, just are search parameters, my question, how to do this "relative search"? Parameters are the checked checkboxes. I'm not sure how to do this. If anyone can help me. An image of checkboxs:

Ithoughtofdoingso,myJS:

varcheckeds=newArray();$("input[name='marcar[]']:checked").each(function (){
   checkeds.push( $(this).val());
});
var obj = $("#paramsPesquisa");
if($(obj).find("input[id='cd_seq_pedido']:checked").length > 0){
    var cd_seq_pedido = $(obj).find("input[id='cd_seq_pedido']:checked").val();
}else{
    var cd_seq_pedido = "";
}

Ajax Method:

   $.ajax({
      type: "POST",
      url: "/pedidoOnline/index.php/Pedidos/checkbox_marcados",
      data: {
          'marcar':checkeds,
          'cd_seq_pedido': cd_seq_pedido,
          'cd_pedido': cd_pedido
      },
      success: function(data){
        console.log(data);
      } 
    });

And then in the controller, how to mount this "relative" Query because it will only be what the user selects, with only 1 result selected by checkbox being possible.

I want to bring these fields, but how do I put the conditions here?

   $pesquisa = $this->PesquisaPedOnline->find('all', array(
            'fields' => array('cd_seq_pedido', 'cd_pedido', 'ds_cpl_tamanho_interno'),
            'conditions' => array(
                    ?????
            )
    ));'
    
asked by anonymous 11.05.2015 / 21:42

2 answers

0

Edson, a suggestion would be to do the following in the cake:

$params = $this->request->data;
if(isset($params['pesquisa1'])){ 
    $conditions['Model.campo1'] = $params['pesquisa1']; 
}
if(isset($params['pesquisa2'])){ 
    $conditions['Model.campo2'] = $params['pesquisa2']; 
}
// assim sucessivamente com todos os campos

// depois joga isso no FIND
$pesquisa = $this->PesquisaPedOnline->find('all',
    array(
        'fields' => array('cd_seq_pedido', 'cd_pedido', 'ds_cpl_tamanho_interno'),
        'conditions' => $conditions // assim
    )
);

I do this every time I have several parameters and they are not required.

    
17.06.2015 / 21:12
0

SQL

CREATE TABLE tb_teste(
    cd_pedido INTEGER,
    cd_seq_pedido INTEGER,
    cd_marca INTEGER,
    cd_tipo INTEGER
);

Promisefully set your inputs with the column name in the table

HTML

<form id="form_pesquisa" method="post">
<?php
    $marcas = array(
        1 => 'TESTE 1',
        2 => 'TESTE 2',
        3 => 'TESTE 3',
        4 => 'TESTE 4',
        5 => 'TESTE 5',
        6 => 'TESTE 6',
    );
    foreach($marcas as $cdMarca => $dsMarca){
        echo sprintf('<p><input name="cd_marca[]" value="%s" type="checkbox"/><label>%s</label></p>', $cdMarca, $dsMarca);
    }
?>
    <input type="submit"/>
</form>

PHP

$post = $_POST;

$conditions = array();
if(isset($post['cd_marca'])){
    $conditions[] = sprintf('Model.cd_marca IN (%s)', implode(',', $post['cd_marca']));
}

# REALIZA A PESQUISA EM BANCO
$pesquisa = $this->PesquisaPedOnline->find('all', array(
    'fields' => array('cd_seq_pedido', 'cd_pedido', 'ds_cpl_tamanho_interno'),
    'conditions' => $conditions,
));

The problem with this method is that you would have to isset for each filtered column, now if you want to make it more dynamic you can still do it through a double foreach, however it will be necessary to implement another indice in inputs

HTML

<form id="form_pesquisa" method="post">
<?php
    $dadosPesquisa = array(
        'cd_marca' => array(
            1 => 'TESTE 1',
            2 => 'TESTE 2',
            3 => 'TESTE 3',
            4 => 'TESTE 4',
            5 => 'TESTE 5',
            6 => 'TESTE 6',
        ),
        'cd_tipo' => array(
            1 => 'TESTE 1',
            2 => 'TESTE 2',
            3 => 'TESTE 3',
            4 => 'TESTE 4',
            5 => 'TESTE 5',
            6 => 'TESTE 6',
        )
    );  

    foreach($dadosPesquisa as $campo => $dados){
        foreach($dados as $cd => $ds){
            echo sprintf('<p><input name="pesquisa[%s][]" value="%s" type="checkbox"/><label>%s</label></p>', $campo, $cd, $ds);
        }
    }
?>
    <input type="submit"/>
</form>

PHP

$post = $_POST;

$conditions = array();
if(isset($post['pesquisa'])){
    foreach($post['pesquisa'] as $campo => $dados){
        $conditions[] = sprintf('Model.%s IN (%s)', $campo, implode(',', $dados));
    }
}
    
19.08.2015 / 14:49