filter with if php and show only if it is greater than 0

2

With this function I create the search for my earrings.

function pendientesporrevisar($conexion){

        $pendientes = (mysqli_query($conexion, "SELECT COUNT(*) AS conteo FROM ticket WHERE tipo_solicitud = 1 && status = 1 ")) or die("Error mostrando  tickets  pendientes: ".mysqli_error($conexion));
        $resultados = mysqli_fetch_row($pendientes);
        return $resultados[0];

    }

I then import it into my html and show it like this

<div align="" class="">PENDIENTES:<?php echo '<div class="notificacion">', $ticket->pendientesporrevisar($conexion),'</div>'; ?></div>

How can I show the number only if it is greater than 0? I mean, do not show me a 0.

    
asked by Juan Ortiz 18.04.2018 в 17:55
source

4 answers

3

Try storing the result in a variable the result and then do the check if it is greater:

<?php

 $resultado = $ticket->pendientesporrevisar($conexion);

 if($resultado > 0) // validamos si es mayor a 0
{
?>
   <div align="" class="">PENDIENTES:<?php echo '<div 
   class="notificacion">',$resultado,'</div>'; ?></div>

<?php } ?>
    
answered by 18.04.2018 / 18:04
source
1

You can use ternary operators as I show you in the next line, I have declared a variable to make the code more readable:

<?php 
    $totalPendiente = $ticket->pendientesporrevisar($conexion);
?>
<div align="" class="">PENDIENTES:
   <?php echo '<div class="notificacion">', 
     $totalPendiente ? $totalPendiente : '' ,
   '</div>'; ?>
</div>
    
answered by 18.04.2018 в 18:08
1

you could use something like this:

$result = mysqli_query("SELECT COUNT(*) AS conteo FROM ticket WHERE tipo_solicitud = 1 && status = 1");

    if(mysqli_fetch_array($result) !== false)
        return 'notificaciones';//hacer lo que deseas !!

is a way you could do it .. I hope I have helped you and luck!

    
answered by 18.04.2018 в 18:10
1

You can determine it in the source function.

Something like this:

function pendientesporrevisar($conexion){

        $pendientes = (mysqli_query($conexion, "SELECT COUNT(*) AS conteo FROM ticket WHERE tipo_solicitud = 1 AND status = 1 ")) or die("Error mostrando  tickets  pendientes: ".mysqli_error($conexion));
        $resultados = mysqli_fetch_row($pendientes);
        $total = ($resultados && $resultados[0] > 0) ? $resultados[0] : '' ;
        return $total;

    }

The ternary operator will return the total when it is greater than 0 or a blank string. Also, it would control any errors due to the use of mysqli_fetch_row . You can also return NULL instead of a blank string ( NULL is not printed on the screen and could be used for any other evaluation in another context):

        $total = ($resultados && $resultados[0] > 0) ? $resultados[0] : NULL ;

Thus, you do not need to manipulate the code again in the call, writing it as is:

<div align="" class="">PENDIENTES:<?php echo '<div class="notificacion">', $ticket->pendientesporrevisar($conexion),'</div>'; ?></div>

I hope it serves you.

    
answered by 18.04.2018 в 18:21