Restart counter to 1 in php

0

I have a question to see if someone can help me In what I am developing I need that on day 1 of each month for example the number of breakdowns starts with 1 and then of course I will add 1 consecutively. Already for day 2 is to get the last and go adding 1, I have echo to get the last one and I add 1 but in the reset on day 1 I do well in the first but then I kept the 1, I'm half tangled with that.

I have this code

$dia = date("d");

foreach ($max_id as $maximo):
//Obtengo el numero maximo de averia
$maxID = $maximo['nro_averia'];
//extraigo los 3 ultimos digitos que es lo que necesito
$nueva_averia = substr($maxID, -3);

 $contador=0;
 //Si es el dia es 1 lo pongo a 1
 if ($dia == 1 && $contador==0){
    $nueva_averia = 1;
    $contador=1;
 } else {
    $nueva_averia += 1 ;
 }
 endforeach;

Greetings and thanks in advance

    
asked by elyera 24.07.2018 в 12:24
source

2 answers

0

I'd better develop it step by step, if you did not understand

This is the table that I will call config and now we go with the code

conect.php

<?php
    //********************************
    //   Y U R I C O
    //********************************
    $connect = 'mysql:host=localhost;dbname=ayuda';
    try {
            $db = new PDO($connect,'root','ascent');
            $db->setattribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        } 
    catch (PDOException $e) {
            echo $e->getMessage();
        }   
?>

e1.php

<?php
    //********************************
    //   Y U R I C O
    //********************************

    include ('conect.php');

    $configSQL = 'SELECT mes,averia FROM config';
    $config = $db->prepare($configSQL); 
    $config -> execute();
    $db_mes='';
    $db_averia='';

    $dia = substr('00'.date("d"),-2);
    //$mes = substr('00'.'08',-2);   Simules el mes y lo pruebes
    $mes = substr('00'.date("m"),-2);


    while ($resp = $config -> fetch (PDO::FETCH_OBJ)) { 
            $db_mes = $resp->mes;
            $db_averia = $resp->averia;
    }               

    if ($db_mes!=$mes){
        $db_averia=1;
        $db_mes=$mes;
    }else{
        $db_averia = $db_averia + 1;        
    }
    $actualizaSQL = 'UPDATE config SET mes = :MES , averia=:AVERIA';
    $actualiza = $db->prepare($actualizaSQL); 
    $actualiza -> bindValue(':MES', $db_mes, PDO::PARAM_STR);
    $actualiza -> bindValue(':AVERIA', $db_averia, PDO::PARAM_STR);
    $actualiza -> execute();

    $codigo = $dia.'-'.$mes.'-'.substr('0000'.$db_averia,-4);
    echo "codigo -> ".$codigo."<br>";

    // aqui ya tu lo guardas las averias en otra tabla
 ?> 

That's how it is, I hope it serves you, goodbye

    
answered by 26.07.2018 / 10:55
source
0

friend I hope this illustration serves you for your better understanding you can adapt it.

<?php
    // averia =   99  999 
    //           dia  incremental   
    $dia = date("d");
    echo "Dia->".$dia."<br>";
    $d = rand(1,31);
    echo "---------<br>";
    echo "Dia Simulado->".$d."<br>";
    echo "---------<br>";   
    if ($dia!=1){
        $averia=rand(0,998); // simula lo que va acumulando la averia es to es a 333 digitos
    }
    if ($dia==1){
        $averia=0;
    }
    $averia++;
    $codigoaveria = substr('00'.$d,-2).substr('000'.$averia,-3);
    echo "Codgio de Averia -> ".$codigoaveria;
    echo "<br>";
    echo "Codigo Para uso -> ".substr('000'.$codigoaveria,-3);
 ?>
    
answered by 24.07.2018 в 19:44