Does not validate conditional with array in php

0

I have a form that adds the total amount of all the installments. I put a conditional where it tells me if the array has data print me the total otherwise I print a message.

I do not know what my error is, but it does not print the message. I continue to print the array with value but obvious emptied.

Here's the code I have:

reports.php

 <!DOCTYPE html>
 <html lang="en">
 <head>
 <meta charset="UTF-8">
 <title>Reportes</title>
 <?php include 'inc/head_comun.php'; ?>
 </head>
 <body>
 <?php include 'inc/menu.php'; ?>

<div class="container" >
<br>
<div class="panel panel-success">
  <div class="panel-heading" align="center"><h3>REPORTES</h3></div>
  <div class="panel-body">

    <form class="form-inline" align="center" method="POST" action="reportes.php">
      <div class="form-group">
        <label for="nombre" >Fecha inicial: </label>
        <input class="form-control " id="nombre" type="date" name="fecha_in" value="<?php echo date("Y-m-d");?>"/>
      </div>
      &nbsp;&nbsp;&nbsp;
      <div class="form-group">
        <label for="email">Fecha final: </label>
        <input class="form-control"  id="email" type="date" name="fecha_fin" value="<?php echo date("Y-m-d");?>"/>
      </div>
      <button type="submit" class="btn btn-success" name="buscar">Buscar</button>
    </form>
    <br>

    <?php

    require_once "modelo/Conectar.php";

    $con=Conectar::conexion();

    if (isset($_POST["buscar"])) {

      $fecha_inicio=$_POST["fecha_in"];
      $fecha_final=$_POST["fecha_fin"];

      $sql=$con->prepare("SELECT TRUNCATE(SUM(cantidad),3) as total from abono Where fecha BETWEEN :fecha_in AND :fecha_fin");
      $sql->execute(array(':fecha_in'=>$fecha_inicio, ':fecha_fin'=>$fecha_final));
      $filas=$sql->fetchAll();

if (count($filas)>0){

echo "hay".var_dump($filas);
}else{
echo "esta vacia ".var_dump($filas);
}


    /*  if (count($filas)>0) {

       foreach ($filas as $registros){

        ?>

        <div class="form-group form-inline" align="center"  style="border-image: initial; border: 1px solid green;">
          <h3>Total cobro entre fechas:
            <span class="label label-warning"> <?php echo $registros["total"];?> $
            </span></h3>
          </div>

          <?php
        }

      }else{
        este es el mensaje que no me imprime cuando no hay abonos con las fechas ingresadas
        echo "No hay abonos en las fechas indicadas";
      }*/

    }

    ?>

    <?php include 'inc/piePagina.php' ?>
    <?php include 'inc/footer_comun.php'; ?>

  </body>
  </html>

The result of the conditional when it is greater than zero of var_dump is: array(1) { [0]=> array(2) { ["total"]=> string(6) "90.000" [0]=> string(6) "90.000" } } hay

Otherwise the result you print is: array(1) { [0]=> array(2) { ["total"]=> NULL [0]=> NULL } } hay

    
asked by Juanzu 06.07.2017 в 17:56
source

1 answer

2

The fetchAll method of PDO returns a array . Therefore $filas=$sql->fetchAll(); creates variable $filas of type array .

To know how many elements of an array you should use the count function.

The comparison of if is wrong. That comparison always gives true.
You must replace your if line with this ...

if (count($filas)>0) {

Update

At first it seemed that it was only a problem that you did not consider the return to be an array, however there is another drawback with the type of query you make to the database.

The aggregate function SUM always returns at least one record. In this particular case where there is no GROUP BY will return only one always.

  • If one or more records meet the condition, return the sum of the same
  • If no record fulfills the condition returns the value NULL

See if the number of elements is greater than 0 will always give true and is wrong in logic. Therefore, the query you must ask is whether the return value in the array is null or not.
Something like ...

if (is_null($filas[0][0])) { // Aquí va la parte donde ningún registro cumplió la condición
    echo "No hay abonos en las fechas indicadas";
}
else { // Aqui va la parte donde obtienes una suma
     foreach ($filas as $registros){ // En realidad aquí habrá un solo registro con ese query. La suma de los abonos.
        // ...
    }
}
    
answered by 06.07.2017 / 19:07
source