Problem with condition

Asked

Viewed 134 times

1

I am trying to create a script that ascertains whether the fine for late payment of the termination is due.

The conditions are as follows::

1 - If the prior notice is indemnified or waived (select), and payment was made within 10 days (date), the fine is not due.

2 - If the prior notice was worked or deducted, and the payment was made after 1 day of dismissal, the fine is due.

3 - In other cases it is not due.

This is the code:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <link rel="stylesheet" href="_css/form2.css"/>
    <title></title>
</head>
<body>
<form method="post" id="Tform" action="multaqss.php">
    <label for="Cavis"><b>Aviso prévio</b><br>Como deve ser considerado o aviso prévio?</label><br>
    <select name="Tavis" id="Cavis">
        <option></option>
        <option value="indenizado">indenizado</option>
        <option value="dispensado">dispensado</option>
        <option value="trabalhado">trabalhado</option>
        <option value="descontado">descontado</option>
    </select>
<br>
    <label id="pagtss"><b>Desligamento</b></label><br>
    <label for="Cdesl22">Qual a data do desligamento?</label><br>
    <input type="date" name="Tdesl" id="Cdesl22" size="6"><br>
    <label id="datapagto"><b>Data do pagamento</b></label><br>
    <label for="Cdatapgtos">Qual a data em que foi realizado o pagamento?</label><br>
    <input type="date" id="Cdatapgtos" name="Tdatapagtos"><br>
    <input type="submit" value="enviar">
</form>
</body>
</html>
<?php

$tavis = $_POST ["Tavis"];
$fimcon = $_POST ["Tdesl"];
$pagtos = $_POST ["Tdatapagtos"];
$multaqss = false;
$prazo = date_diff(date_create($pagtos), date_create($fimcon))->format('%d');

    if ($tavis == "indenizado" || "dispensado" && $prazo > 10){
        $multaqss = true;
    }
    elseif ($tavis == "trabalhado" || "descontado" && $prazo > 1) {
        $multaqss = true;
    }

print "A data de desligamento foi $fimcon" . "<br>";
print "A data de pagamento foi $pagtos" . "<br>";
print "A diferença entre o desligamento e o pagamento é de $prazo dia(s)" . "<br>";
print "O aviso prévio foi $tavis" . "<br>";

    if ($multaqss == false) {
        echo "A multa não é devida" . "<br>";
    }
    else {
        echo "A multa é devida." . "<br>";
    }
?>

The problem is that no matter what I do, he always considers it due, even if the date doesn’t match.

An output example (wrong):

The shutdown date was 2015-04-01

Payment date was 2015-04-05

The difference between disconnection and payment is 4 day(s)

Notice has been indemnified

The fine is due.

  • 1

    I don’t know much about php, but you shouldn’t separate operators using parentheses, for example, in this expression ? ($tavis == "indenizado" || "dispensado" && $prazo > 10)

  • Aside from having some less visible problem of date format, it seems to be just this.

  • 1

    It would be nice to @Carlos reply.

  • In a if you put "true" (string) and the other true (Boolean). This is not the problem but it is good to be careful with these details

  • Thanks Lucas, that was a mistake really, I was trying to switch to string to see if it worked, and I had changed all, then when I returned to true/false I forgot these quotes. I already fixed here, and made the changes that indicated below (parentheses) but not yet solved.

  • @bigown What may have gone wrong with the date format?

  • I don’t know, it’s always a possibility for whoever’s looking at the code but there’s no way to test it. Could be something else, this was just an example, not everything gives p/ identify just by looking at the code. The code has a lot of small problems but here in the commentary it is difficult to cite all. Even if they are not causing actual errors, it shows that there may be other things wrong without being visible.

  • I put the script in the codepad (http://codepad.org/Lz3IWpUm) and gave this error: Fatal error: Call to Undefined Function date_diff() on line 7. As this was the only thing of the code I got ready (right here in Sopt), I believe the error is there (not that the code is wrong, perhaps the use I gave it...). He gives the right exit from the days of difference, but somehow does not consider it in the condition...

  • Friend just a hint, use isset(...)

Show 4 more comments

1 answer

3


Actually, there is an error in the condition. You must resolve by changing to:

if (($tavis == "indenizado" || $tavis == "dispensado") && $prazo > 10){
    $multaqss = true;
}
elseif (($tavis == "trabalhado" || $tavis == "descontado") && $prazo > 1) {
    $multaqss = true;
}
  • The if is with two parentheses after being discharged", and the elseif is with only one. If I put only one it works, but still gives the same error. If I place two, the other condition is marked as unused by phpstorm. I tried so too: if (($Tavis == "indemnified" || "dismissed") && ($term > 10)) but did not resolve.

  • Actually, there was an extra parenthesis, I corrected. The problem to which I referred is that the comparison was not being made with the "dismissed" nor with the "discounted". So I made the modifications: from '|| "dispensed"' to '|| $Tavis == "dispensed" and from '"|| "discounted"' to '|| $Tavis == "discounted"&#Xa"and only then will it be checked whether the $term is greater than 1 or 10, depending on the case.

  • Thanks, I got the point, and I left it like that in the code, but it still doesn’t work.

  • 1

    Wow! I hadn’t seen you change the condition by putting the variable back after ||... I didn’t know I needed to repeat. Thanks a lot, now it’s working!

Browser other questions tagged

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