Hide date Datatime php 0000-00-00 00:00:00

1

I have the following product table

idproducto  
numero_orden  
modelo  
tipo
fecha
fecha2
status
resultado

the "date" is which the product is registered and "date2" is when the product is delivered or modified, the problem is that when registering a product is shown in the field of "last update" 0000-00- 00 00:00:00 this is my code to register the product

$consulta = mysqli_query($connect, "SELECT * FROM producto ORDER BY idproducto DESC LIMIT 1")
or die ("Error al traer los datos");

$idproducto=1000;

while ($extraido = mysqli_fetch_array($consulta)) {
    $idproducto = $extraido['idproducto'] + 1;
}


$num_orden= $_POST['num_orden'];
$modelo= $_POST['modelo'];
$tipo= $_POST['tipo'];
date_default_timezone_set('America/La_Paz');
$fecha= date("Y-m-d H:i:s"); 
$estatus = $_POST['status'];

$query = mysqli_query($connect, "INSERT INTO producto(idproducto, num_orden, modelo, tipo, fecha, status) 
            VALUES ('$idproducto','$num_orden', '$modelo', '$tipo', '$fecha','$estatus')")

or die ("Error al registrar producto");  

So it looks like this

In my code to show the fields is this

<tr>
            <td style="text-align:center;"><?php echo $data['idproducto']; ?></td>
              <td style="text-align:center;"><?php echo $data['num_orden']; ?></td>
              <td style="text-align:center;"><?php echo $data['modelo']; ?></td>
              <td style="text-align:center;"><?php echo $data['tipo']; ?></td>
              <td style="text-align:center;"><?php echo $data['fecha']; ?></td>
              <td style="text-align:center;"><?php echo $data['fecha2']; ?></td>
              <td style="text-align:center;"><?php
switch ($data['status']) {
case "En espera":
    echo "En espera";

    break;


case "En proceso":
    echo "En proceso";
    break;
case "En proceso con demora":
    echo "En proceso con demora";
    break;
case "Finalizado":
    echo "Finalizado";
    break;
case "Finalizado sin reparar":
    echo "Finalizado sin reparar";
    break;
}
?>

              </td>
              <td style="text-align:center;"><?php echo $data['resultado']; 
?></td>
              <td>

So how can I make it so that it only shows the value of date2 only when it is modified there?

    
asked by Gndx 27.10.2018 в 03:55
source

3 answers

0

Welcome to Stackoverflow.

Apart from the problem you have, and that can be solved as indicated by @JuanPinzon, your code can be optimized in several points:

  • You could avoid mixing PHP / HTML code, which although valid, produces an unreadable code, difficult to debug because it is confusing and horrible
  • The switch ... case you're using does not make any sense.

It also seems strange how you fill the array after SELECT , but since I do not know the context I can not tell you more about that.

To improve at least the two points mentioned above, we can write the code like this:

<?php
    /*
        Usaremos una variable llamada $html 
        para concatenar las partes de la tabla
        dado que tu código es presentado parcialmente
        conviene que crees la variable 
        donde empiezas a construir la tabla
        la idea principal es que evites mezcla PHP/HTML
    */
    $html="<table>";

    /*
        Guardamos referencias a cada dato
    */
    $idproducto=$data['idproducto'];
    $num_orden=$data['num_orden'];
    $modelo=$data['modelo'];
    $tipo=$data['tipo'];
    $fecha=$data['fecha'];
    $status=$data['status'];
    $resultado=$data['resultado'];

    /*
        Para determinar el valor de la fecha2
        usamos un ternario, para hacerla valer
        una cadena vacía cuando no haya fecha
    */

    $fecha2=($data['fecha2'] == '0000-00-00 00:00:00') ? "" : $data['fecha2'];

    /*
        Guardamos referencia a esto que se repite una y otra vez
        Conviene señalar que es una mala práctica poner reglas
        de estilo directamente en los elementos
        La práctica recomendada es poner nombres de clases o selectores
        y aplicar el estilo por CSS
    */
    $_td='<td style="text-align:center;">';
    $td_="</td>";

    /*
        Empezamos a concatenar
        Nótese que en PHP se concatena con .=
    */
    $html.="<tr>";
        $html.="$_td$idproducto$td_";
        $html.="$_td$num_orden$td_";    
        $html.="$_td$modelo$td_";   
        $html.="$_td$tipo$td_"; 
        $html.="$_td$fecha$td_";    
        $html.="$_td$fecha2$td_";   
        $html.="$_td$status$td_";   
        $html.="$_td$resultado$td_";
    $html.="</tr>";

    /*
        Cerrar la tabla donde haya que cerrarla
    */
    $html.="</table>";

    /*
        Imprimir la tabla donde haya que imprimirla
    */
    echo $html;
?>

I hope it will be useful. The code was made long by the comments. If you remove them you will be able to better appreciate their simplicity and clarity.

    
answered by 27.10.2018 / 10:23
source
0

If your column date2 does not accept null, what you could do is compare the text and change the value of the 00:00:00 value for emptiness, something like this:

Before presenting date2 place this condition

<?php
if($data['fecha2'] == '0000-00-00 00:00:00'){
  $data['fecha2'] = "";
}
?>

And then you present the date

<td style="text-align:center;"><?php echo $data['fecha2']; ?></td>

Or if you want to make it more elegant, you can do it with a ternary operator

<td style="text-align:center;"><?php echo ($data['fecha2'] == '0000-00-00 00:00:00'?'':$data['fecha2']); ?></td>
    
answered by 27.10.2018 в 04:52
0

Another option would be:

1) Allow the date of modification to accept null values, since not all the insertions you make will undergo modifications later.

2) When retrieving the data to show them in the list, use the "case" statement to indicate any text that indicates that you have not modified it yet. SELECT (case WHEN fecha2 is null THEN 'SIN MODIFICACION' ELSE fecha2 END) AS FECHA2
FROM productos

    
answered by 27.10.2018 в 04:57