Shows days with pouch ignoring today with return JSON

Asked

Viewed 51 times

-1

I’m trying to make a logic to display the next day that a particular warehouse will have mailing pouch, I’m trying to create a array with the quest I’m making in my banco de dados and trying to ignore today and always see the next day, but without success. In my bank the days are 0 and 1, and there will be no pouch and 1, yes.

My bank is like this:

Imagem do banco de dados

My code is like this:

if (GetApplication()->isGetValueSet('IdUnicoop')) {
    $IdUnicoop = GetApplication()->GetGETValue('IdUnicoop');
    $sql = "SELECT * FROM dvsMaloteDias WHERE dvsMaloteDias.IdUnicoop = $IdUnicoop";
    $queryResult = $this->GetConnection()->fetchAll($sql);

    $Notificacao = array();

              foreach($queryResult as $RegResult) {           

            if ($RegResult['Segunda'] != 0) {       
                array_push($Notificacao, "Segunda");
            }
            if ($RegResult['Terça'] != 0) {     
                array_push($Notificacao, "Terça");
            }
            if ($RegResult['Quarta'] != 0) {        
                array_push($Notificacao, "Quarta");
            }       
            if ($RegResult['Quinta'] != 0) {        
                array_push($Notificacao, "Quinta");
            }
            if ($RegResult['Sexta'] != 0) {     
                array_push($Notificacao, "Sexta");
            }
            if ($RegResult['Sabado'] != 0) {        
                array_push($Notificacao, "Sábado");
            }
            if ($RegResult['Domimgo']!= 0) {        
                array_push($Notificacao, "Domingo");
            }
            if ($RegResult['Todos'] != 0) {     
                array_push($Notificacao, "Todos");
            }          

        }


    $result = array(
        'Notificacao' => 'O próximo dia do malote é: ' . $Notificacao
    );

    echo json_encode($result);
    exit;                              
}

According to the image, for example, the warehouse 9 has pouch on Monday, but I must check what day is today and if it is Monday, I have to show that there will be pouch on Wednesday and Wednesday, check the day and if it is Wednesday, show Friday.

I even managed to find out the day of the week, but the comparison didn’t, look:

    $now = new DateTime('now', new DateTimeZone('America/Sao_Paulo'));
    $minhadataYmdHis = $now->format('Y-m-d');

    $diaSemanaN= date("w", strtotime($minhadataYmdHis));

    switch($diaSemanaN) {
        case 0:
        $diaSemana="Domingo";
        break;
        case 1:
        $diaSemana="Segunda";
        break;  
        case 2:
        $diaSemana="Terça";
        break;  
        case 3:
        $diaSemana="Quarta";
        break;
        case 4:
        $diaSemana="Quinta";
        break;  
        case 5:
        $diaSemana="Sexta";
        break;          
        case 6:
        $diaSemana="Sábado";
        break;              
    }

    echo $diaSemana;

1 answer

1


Hello.

This is a logic problem. Usually it is not something that should be solved here.

But I will give you a code that works, but is not 100% cut. This code does not check for example when every day has pouch. And there’s one more problem in it that you should solve. If today is Thursday and you have a pouch on Monday, he won’t be able to warn you that.

setlocale(LC_TIME, 'pt_BR', 'pt_BR.utf-8', 'pt_BR.utf-8', 'portuguese');

$simulacaoBanco = [
    [
        "idUnicoop" => 1,
        "Segunda" => 1,
        "Terca" => 0,
        "Quarta" => 1,
        "Quinta" => 0,
        "Sexta" => 1,
        "Sabado" => 0,
        "Domingo" => 0,
        "Todos" => 0
    ]    
];
//percorre todos linha a linha vinda do banco, no exemplo há somente uma linha
foreach($simulacaoBanco as $simulacao){
    $idUnicoop = $simulacao['idUnicoop'];
    unset($simulacao['idUnicoop']);
    $todos = $simulacao['Todos'];
    unset($simulacao['Todos']);
    $simulacao = array_values($simulacao); //reseta o array para indices numericos 
começando de segunda = 0
    $diaSemana = date('w', time()); //pega o dia da semana começando de domingo = 0;
    if($diaSemana === 0){
        $diaSemana = 6; //é o domingo vindo do banco
    }else{
        $diaSemana = $diaSemana - 1; //hoje segunda é = 1 mas no banco é 0 então por 
isso a subtração para igualar os dias
    }
    foreach($simulacao as $diaBanco => $temMalote){
        if($diaBanco > $diaSemana){
            if($temMalote === 1){
                //dia atual + dia vindo do banco = proximo dia com malote
                echo 'Na ' . date('l', strtotime('+' . ($diaBanco) . ' days'  , 
time())) . ' tem malote.' . PHP_EOL; 
            }
        }
    }
}
  • Thanks @Matheusprado for the tip, but I didn’t understand the comment about logic, if I’m having trouble with it and so I posted the code that already.

  • Usually the problems posted here are errors with functions or syntax or who knows with unexpected problems. If you had a code that already did what you need it to do, but it causes errors or works unexpectedly, it would be another story.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.