System check if there were payments from the 1st to the 5th of each month

1

is a condominium payment system and it is required to show if the owners paid in the range of 1 to 5 of each month I have the following but it does not do anything to me, it remains blank and the table does have data

<?php
session_start();
 require_once('../../conexion.php');
    $hoy = getdate();
  
    $begin = new DateTime( '2018-08-01' );
   
    $end = new DateTime( '2018-08-05' );
 
    $end = $end->modify( '+1 day' ); 

   
    $interval = new DateInterval('P1D');
    $daterange = new DatePeriod($begin, $interval ,$end);

    $alerta="";
   
    foreach($daterange as $date){
     
        $fecha_dia= $date->format("Ymd");
     
        $sql = "SELECT * FROM pago_p WHERE fechadeposito ='$fecha_dia'"; 
        $res = mysqli_query($conexion,$sql);

        while($resultado = mysqli_fetch_array($res)){
       
            if(!empty($resultado["pago"])) {
            
                $alerta="si";
            }
        }

        if ($alerta=="si") {
            //despues del ciclo imprimimos la alerta para que en cada ciclo no la repita
            echo '<script> alert("No realizaste el pago en la fecha determinada"); </script>';
        }
    }
?> 

The table is your entity called date of deposit and its type is date

    
asked by Anderson Rey 01.09.2018 в 23:37
source

2 answers

0

The approach has a logic problem. If what you need is those who paid after the 05 of each month you do not need an interval, but the last date and the beginning of the next month, in case it was necessary to make that cut. Otherwise, only the cut-off date, which is 05 of each month, would be required. Here I leave an example of it, where it tells you the id of each user who paid out of term.

<?php
    //Conexion
    $mysqli = new mysqli("localhost", "root", "", "stackoverflow");
    if($mysqli->connect_errno){
        echo "Fallo al conectar a MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
    }

    //Pasar año y mes para la consulta
    $periodo = $_GET['year'].'-'.$_GET['month'];

    //Formate de fecha
    date_default_timezone_set('America/Argentina/Buenos_Aires');
    $dateLimit = new DateTime($periodo.'-05');
    $dateLimit = $dateLimit->format("Y-m-d");

    $nextDate = new DateTime($periodo.'-01');
    $nextDate = $nextDate->modify('+1 month'); 
    $nextDate = $nextDate->format("Y-m-d");

    //Generar la consulta
    $sql = "SELECT id, fechadeposito, pago FROM pago_p WHERE fechadeposito > '$dateLimit' AND fechadeposito < '$nextDate'";

    if (
        //Validar que la consulta arroje resultados
        $resultado = $mysqli->query($sql) AND
        $resultado->num_rows > 0
    ){
        //Convertir cada resultado en un objeto
        while($row = $resultado->fetch_object()){
            printf('El usuario %s no realizó el pago a tiempo.', $row->id);
            echo "<br />";
        }       

    }else{
        echo 'Todos los usuarios pagaron a tiempo.';
    }

    //Liberar resultado
    $resultado->close();

    //Cerrar la conexión
    $mysqli->close();

?>

I hope this answer will help you and we will follow up on any comments. Greetings!

    
answered by 02.09.2018 / 01:54
source
0

Here what there is is a logic problem ... as far as I can see:

foreach($daterange as $date){

        $fecha_dia= $date->format("Y-m-d");

        $sql = "SELECT * FROM pago_p WHERE fechadeposito ='$fecha_dia'"; 
        $res = mysqli_query($conexion,$sql);

        while($resultado = mysqli_fetch_array($res)){

            if(!empty($resultado["pago"])) {
            // si entra aqui es por que el campo no esta vacio, por ende si se realizo el pago
//guardas en una variable donde dices si
                $alerta="si";
            }
        }
//luego cuanto termina preguntas
//obviamente si hubo pago la variable va a valer "si" entonces estas diciendo que no hizo el pago cuando en verdad si lo hizo
        if ($alerta=="si") {
            //despues del ciclo imprimimos la alerta para que en cada ciclo no la repita
            echo '<script> alert("No realizaste el pago en la fecha determinada"); </script>';
        }
    }

solution: if ($alerta !="si") { So you deny the condition, you say "If $ alert is different from" if "then there if refererimo that the payment was not made"

NOTE: guines are added to the format, so that it compares well in BD

you tell me how it went ....

    
answered by 02.09.2018 в 00:50