PHP cycle while printing unwanted results

0

I am making an invoice in which I can pay 1 or several installments of the contract at the same time. Each payment is saved with the date and time it was made. For example if I pay 3 installments at the same time, all 3 have the same time and date.

My table in MySQL is the following:

CREATE TABLE 'alquiler_renta_detalle'
(
   'codigo_factura' int(11) NOT NULL,
   'cuota_renta' text collate utf8_spanish2_ci NOT NULL,
   'fecha_cuota_pagada' date NOT NULL,
   'hora_cuota_pagada' time NOT NULL,
   KEY 'CODIGO_FACTURA' ('codigo_factura')
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_spanish2_ci;


//aqui he realizado dos pagos, el primero de 3 cuotas y el segundo con 2 cuotas

INSERT INTO 'alquiler_renta_detalle' VALUES (2, '1', '2018-03-11', '08:08:02');
INSERT INTO 'alquiler_renta_detalle' VALUES (2, '2', '2018-03-11', '08:08:02');
INSERT INTO 'alquiler_renta_detalle' VALUES (2, '3', '2018-03-11', '08:08:02');
INSERT INTO 'alquiler_renta_detalle' VALUES (2, '4', '2018-03-11', '08:48:48');
INSERT INTO 'alquiler_renta_detalle' VALUES (2, '5', '2018-03-11', '08:48:48');

My problem is when I'm going to show the payment history, it should come out:

Cuotas: 1,2,3
Cuotas: 4,5

But my throws this:

Cuotas: 1
Cuotas: 1,2
Cuotas: 1,2,3
Cuotas: 4
Cuotas: 4,5

Here I leave the code in php

<?php
    //conexion a la base de datos
    include("../conexion.php"); 
    $conec=Conectarse();

    //consulta a la tabla "alquiler_renta_detalle"
    $sql = "SELECT * FROM alquiler_renta_detalle WHERE codigo_factura=2 ORDER BY cuota_renta + 0 ASC";
    $result = mysql_query ($sql);
?>
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>

    <!--si la consulta es correcta comienza el siguiente IF-->
    <?php
        if($result>0) {

            $hora_pasada=0;

            while ($row = mysql_fetch_row($result)){
                //$row[4] representa al campo donde fue guardado la hora en la cuota fue pagada
                if ($row[4]==$hora_pasada) {
                    //$row[2] representa el numero de cuota pagada.
                    $cuotas=$cuotas.",".$row[2];
                }else{

                    $hora_pasada=$row[4];
                    $cuotas=$row[2];
                }

                echo "Cuotas: ".$cuotas."<br>";
            }
        }
    ?>
</body>
</html>

I will be very grateful if someone can help me.

    
asked by Raul Escalona 11.03.2018 в 14:30
source

1 answer

3

You can get quotes directly with an SQL query

SELECT GROUP_CONCAT(cuota_renta SEPARATOR ', ') FROM 'alquiler_renta_detalle' GROUP BY fecha_cuota_pagada, hora_cuota_pagada

Result:

+------------------------------------------+
| GROUP_CONCAT(cuota_renta SEPARATOR ', ') |
+------------------------------------------+
|                                  1, 2, 3 |
|                                     4, 5 |
+------------------------------------------+

Your page would look like this:

<?php
//conexion a la base de datos
include("../conexion.php"); 
$conec=Conectarse();

//consulta a la tabla "alquiler_renta_detalle"
$sql = "SELECT GROUP_CONCAT(cuota_renta SEPARATOR ', ') FROM alquiler_renta_detalle GROUP BY fecha_cuota_pagada, hora_cuota_pagada;";
$result = mysql_query ($sql);
?>
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>

<!--si la consulta es correcta comienza el siguiente IF-->
<?php
    if($result>0) {
        while ($row = mysql_fetch_row($result)){
                //Obtenemos la primera columna, el GROUP_CONCAT
                $cuotas = $row[0]
                echo "Cuotas: ".$cuotas."<br>";
            }

        }
    }
?>

    
answered by 11.03.2018 / 16:27
source