How to use User-informed search conditions ( Not required )

Asked

Viewed 62 times

1

My doubt is a little complicated to explain, but come on. I have several Checkboxs, where it is not mandatory to select them to do the search, are only search parameters, my question, how to do this search "relative"? The parameters are the selected checkboxes. I’m not sure how to do this. If anyone can help me. A picture of the checkboxs : inserir a descrição da imagem aqui

Thought I’d do it like this, my JS:

var checkeds = new Array();
$("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, being only possible 1 result selected by checkbox.

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

   $pesquisa = $this->PesquisaPedOnline->find('all', array(
            'fields' => array('cd_seq_pedido', 'cd_pedido', 'ds_cpl_tamanho_interno'),
            'conditions' => array(
                    ?????
            )
    ));`
  • Dude, but what exactly are the data you’re looking for, what Oce wants to display with this data?

2 answers

0

SQL

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

Prominently define 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’d have to make a isset for each filtered column, now if you want to leave it more dynamic you can still perform through a double foreach, however it will be necessary to implement one more indice us 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));
    }
}

0

Edson, a suggestion would be to do as follows 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 it this way whenever I have several parameters and they are not mandatory.

Browser other questions tagged

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