Download with PHP filters

1

I want to be able to make a download with filters. So far I get to download by city, state or city by state or vice versa, but not download me by a date range, or city or state according to date. In a few words I want to download by a single field or by the combination of all the fields. The code I have is the following.

PHP

$DESCARGAR = $_POST['descargar'];
$CIUDAD = $_POST['CIUDAD'];
$ESTADO = $_POST['ESTADO'];
$FECHA_DESDE = $_POST['FECHA_DESDE'];
$FECHA_HASTA = $_POST['FECHA_HASTA'];

if(isset($_POST['descargar'])) {

    if(empty($_POST['CIUDAD']))
    {
        $WHERE = "WHERE ESTADO = '".$ESTADO."'";
    }
    else if(empty($_POST['ESTADO']))
    {
        $WHERE = "WHERE CIUDAD = '".$CIUDAD."'";    
    }
    else if(empty($_POST['ESTADO']) && empty($_POST['CIUDAD']))
    {
        $WHERE = "WHERE FECHA_TANQUEO BETWEEN '".$FECHA_DESDE."' AND ".$FECHA_DESDE."'";    
    }
    else
    {
        $WHERE = "WHERE CIUDAD = '".$CIUDAD."' AND ESTADO = '".$ESTADO."'"; 
    }

}

$consulta= "SELECT * FROM combustible $WHERE";
$resultado= $mysqli->query($consulta);

header('Content-type: application/vnd.ms-excel');
header("Content-Disposition: attachment; filename=REPORTE DE COMBUSTIBLE ".$fecha.".xls");
header("Pragma: no-cache");
header("Expires: 0");

<?php
//Código para exportar a Excel, este código funciona correctamente
?>

HTML

 <input type="text" name="FECHA_DESDE" id="FECHA_DESDE" class="tcal">
 <input type="text" name="FECHA_HASTA" id="FECHA_HASTA" class="tcal">

  <select name="CIUDAD" id="CIUDAD" >
    <option value="">Seleccione...</option>
    <option value="ARMENIA">ARMENIA</option>
    <option value="CALI">CALI</option>
  </select>

  <select name="ESTADO" id="ESTADO">
    <option value="">Seleccione...</option>
    <option value="PENDIENTE">PENDIENTE</option>
    <option value="RECIBIDO">RECIBIDO</option>
  </select>
    
asked by Anderviver 15.01.2018 в 17:46
source

1 answer

2

Let's see if this suits you.

The code has the following logic:

  • We use ternary operators to kill two birds with one stone: we verify if the data exists and if it exists we build criteria that we will then pass in WHERE

  • We make a more logical evaluation of the possible WHERE . I do not know why you pose a logic somewhat convoluted in the code of the question. The logic I have followed is as follows: if a value exists in POST is because that value must be in WHERE , period . As for the two dates, I verify that both have values, if not, I do not believe the WHERE with the BETWEEN . I have assumed that the fields CIUDAD and ESTADO are of type VARCHAR or other non numeric. If they are numeric, you must remove the single quotes that enclose the criteria.

  • At the end we use implode to create all the criteria, joining them with AND .

  • Control how you have the block if(isset($_POST['descargar'])) { , the launch of the query can not go outside that if .

  • Everything else will depend on your code.

    I hope it serves you:

    if(isset($_POST['descargar'])) {
    
        $arrWhere = [];
        $strWhere="";
    
        $strCiudad = empty($_POST['CIUDAD']) ? NULL : " CIUDAD='".$_POST['CIUDAD']."'";
        $strEstado = empty($_POST['ESTADO']) ? NULL : " ESTADO='".$_POST['ESTADO']."'";
        $strFechaDesde = empty($_POST['FECHA_DESDE']) ? NULL : " FECHA_TANQUEO BETWEEN '".$_POST['FECHA_DESDE']."'";
        $strFechaHasta = empty($_POST['FECHA_HASTA']) ? NULL : " AND '".$_POST['FECHA_HASTA']."'";
    
        if ($strCiudad)
        {
            $arrWhere[] = $strCiudad;
        }       
        if ($strEstado)
        {
            $arrWhere[] = $strEstado;
        }       
        if ($strFechaDesde && $strFechaHasta)
        {
            $arrWhere[] = $strFechaDesde.$strFechaHasta;
        }
    
        if ($arrWhere)
        {
            $strWhere=' WHERE '.implode(' AND ', $arrWhere);
        }
    
        $strSQL= "SELECT * FROM combustible ".$strWhere;
        $resultado= $mysqli->query($strSQL);
        //Resto del código  
    
    }else{
    
    echo "No se pasó el POST descargar";
    
    }
    
      

    Note: Query $strSQL= "SELECT * FROM combustible ".$strWhere; should not be passed directly to the base handler of   data, since the variable $strWere could be changed by code   harmful to the database and to the system. The code has been   written in this way only to respond to the concrete problem of   criteria. However, with PDO, this procedure would be more secure   and easier to perform .

        
    answered by 15.01.2018 / 19:05
    source