Filter of the month and year of a date via combobox

Asked

Viewed 278 times

0

Galera is the following I have a table in my database where I have the data_process field that is in date format, so every time I register a process in the process registration form I select a date to register such process and the other fields... So far so good.
But I am creating a filter via combobox, to search the processes in the following ways:

Month or year This way if I select only one of the two fields, will be displayed all processes of the selected month independent of the year, the same thing happens when I select the year ie, is displayed all processes of the selected year idependente of the month.

Month and year This way if I select a month and a year only the processes of the selected month and year will be displayed

It follows the code that I have already managed to do, so when I select only the month, all the processes of the selected month are displayed independently of the year, but when I select only the year nothing is shown, and when I select the two options it works correctly, or is displayed the processes of the selected month and year. The search just doesn’t happen when I select only the year. Someone can give a force?

Obs: I’m using php, mysql

<form name="fmrpesquisa" id="frmpesquisa">
      <div class="row">
        <div class="col-sm-6" style="">
          <select name="parametro_mes" id="parametro_mes" class="form-control">
            <option value="">MÊS</option>
            <option value="01">01</option>
            <option value="01">02</option>
            <option value="03">03</option>
            <option value="04">04</option>
            <option value="05">05</option>
            <option value="06">06</option>
            <option value="07">07</option>
            <option value="08">08</option>
            <option value="09">09</option>
            <option value="10">10</option>
            <option value="11">11</option>
            <option value="12">12</option>
          </select>
        </div><br>

        <div class="col-sm-5" style="">
          <select name="parametro_ano" id="parametro_ano" class="form-control">
            <option value="">ANO</option>
            <option value="2019">2019</option>
            <option value="2018">2018</option>
            <option value="2017">2017</option>
          </select>
        </div><br>

        <div class="col-sm-1" style="">
          <input type="submit" value="BUSCAR">
        </div>
      </div><br>
    </form>

<?php
$parametro_ano = filter_input(INPUT_GET, "parametro_ano");
$parametro_mes = filter_input(INPUT_GET, "parametro_mes");

$sql = "SELECT tb_processo.codigo_processo,tb_processo.numero_processo,tb_processo.data_processo,tb_processo.assunto
            FROM 
            tb_processo
            WHERE(MONTH(data_processo) = '$parametro_mes' AND  YEAR(data_processo) = '$parametro_ano')";
$recebe = mysqli_query($con, $sql);
  • 1

    Dude you will have to generate your query dynamically. Sua variável $sql terá ir apenas até SELECT tb_processo.codigo_processo,tb_processo.numero_processo,tb_processo.data_processo,tb_processo.assunto FROM tb_processo . And then you check if $parametro_ano is different from null if it is you insert it in the query the same for $parametro_mes and build the query dynamically. 'Cause when you select only the year, for the bank you’re asking for a lawsuit with the year = 2019 with a null month and then he doesn’t really think...

1 answer

0


<?php
$sql = "SELECT tb_processo.codigo_processo,
               tb_processo.numero_processo,
               tb_processo.data_processo,
               tb_processo.assunto
          FROM tb_processo
         WHERE 1 = 1";

if ( !empty($_REQUEST['parametro_ano']) )
   $sql .= " AND YEAR(data_processo) = ".$_REQUEST['parametro_ano'];

if ( !empty($_REQUEST['parametro_mes']) )
   $sql .= " AND MONTH(data_processo) = ".$_REQUEST['parametro_mes'];

echo $sql;

?>
<form name="fmrpesquisa" id="frmpesquisa">
      <div class="row">
        <div class="col-sm-6" style="">
          <select name="parametro_mes" id="parametro_mes" class="form-control">
            <option value="">MÊS</option>
            <option value="01">01</option>
            <option value="01">02</option>
            <option value="03">03</option>
            <option value="04">04</option>
            <option value="05">05</option>
            <option value="06">06</option>
            <option value="07">07</option>
            <option value="08">08</option>
            <option value="09">09</option>
            <option value="10">10</option>
            <option value="11">11</option>
            <option value="12">12</option>
          </select>
        </div><br>

        <div class="col-sm-5" style="">
          <select name="parametro_ano" id="parametro_ano" class="form-control">
            <option value="">ANO</option>
            <option value="2019">2019</option>
            <option value="2018">2018</option>
            <option value="2017">2017</option>
          </select>
        </div><br>

        <div class="col-sm-1" style="">
          <input type="submit" value="BUSCAR">
        </div>
      </div><br>
    </form>

Browser other questions tagged

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