Collectif H2Fr

Communauté Web & Forum de Sécurité Informatique

-40%
Le deal à ne pas rater :
-40% sur le Pack Gaming Mario PDP Manette filaire + Casque filaire ...
29.99 € 49.99 €
Voir le deal

    Gérer les "if" imbriqués en PHP

    Locust
    Locust
    Newbie
    Newbie

    Messages : 38
    Date d'inscription : 22/08/2015
    Age : 32
    Localisation : Île de France

    Gérer les "if" imbriqués en PHP Empty Gérer les "if" imbriqués en PHP

    Message par Locust Sam 12 Mar - 20:01

    Bonjour à tous

    je développe un calculateur de points pour un jeu, et je recontre un problème avec les conditions "if" / "else" imbriquées

    Code:
    <form method="POST">
    <p><label>Vos points : <input type="text" name="startPoint" /></label></p>
    <p><label>Points adversaire : <input type="text" name="enemyPoint" /></label></p>
    <p><label>Victoire :
    <input type="radio" name="victoire" value="oui" checked="checked" /> <label for="oui">Oui</label>
    <input type="radio" name="victoire" value="non" /> <label for="non">Non</label></p>
    <p><label>Coefficient :
    <input type="radio" name="coef" value="0.5" /> <label for="0.5">0.5</label>
    <input type="radio" name="coef" value="0.75" /> <label for="0.5">0.75</label>
    <input type="radio" name="coef" value="1" checked="checked" /> <label for="0.5">1</label>
    <input type="radio" name="coef" value="1.25" /> <label for="0.5">1.25</label>
    <input type="radio" name="coef" value="1.50" /> <label for="0.5">1.50</label>
    </p>
    <p><input type="submit" value="Calculer" /></p>
    </form>
    <?php
    $victoire = $_POST['victoire'];
    $startPoint = $_POST['startPoint'];
    $enemyPoint = $_POST['enemyPoint'];
    $diff = $startPoint - $enemyPoint;
     
    if ($victoire == "oui") {
     
        if ($diff >= 0 && $diff <= 24) {
            if ($startPoint >= $enemyPoint) {$gain = 6;}
            else {$gain = 6;}
        }
     
        if ($diff >= 25 && $diff <= 49) {
            if ($startPoint >= $enemyPoint) {$gain = 5.5;}
            else {$gain = 7;}
        }
        
        if ($diff >= 50 && $diff <= 99) {
            if ($startPoint >= $enemyPoint) {$gain = 5;}
            else {$gain = 8;}
        }
        if ($diff >= 100 && $diff <= 149) {
            if ($startPoint >= $enemyPoint) {$gain = 4;}
            else {$gain = 10;}
        }
        if ($diff >= 150 && $diff <= 199) {
            if ($startPoint >= $enemyPoint) {$gain = 3;}
            else {$gain = 13;}
        }
        if ($diff >= 200 && $diff <= 299) {
            if ($startPoint >= $enemyPoint) {$gain = 2;}
            else {$gain = 17;}
        }
        if ($diff >= 300 && $diff <= 399) {
            if ($startPoint >= $enemyPoint) {$gain = 1;}
            else {$gain = 22;}
        }
        if ($diff >= 400 && $diff <= 499) {
            if ($startPoint >= $enemyPoint) {$gain = 0.5;}
            else {$gain = 28;}
        }
        if ($diff >= 500) {
            if ($startPoint >= $enemyPoint) {$gain = 0;}
            else {$gain = 40;}
        }
        }
        
    elseif ($victoire == "non") {
        
        if ($diff >= 0 && $diff <= 24) {
            if ($startPoint <= $enemyPoint) {$gain = -5;}
            else {$gain = -5;}
        }
        if ($diff >= 25 && $diff <= 49) {
            if ($startPoint <= $enemyPoint) {$gain = -4.5;}
            else {$gain = -6;}
        }
        if ($diff >= 50 && $diff <= 99) {
            if ($startPoint <= $enemyPoint) {$gain = -4;}
            else {$gain = -7;}
        }
        if ($diff >= 100 && $diff <= 149) {
            if ($startPoint <= $enemyPoint) {$gain = -3;}
            else {$gain = -8;}
        }
        if ($diff >= 150 && $diff <= 199) {
            if ($startPoint <= $enemyPoint) {$gain = -2;}
            else {$gain = -10;}
        }
        if ($diff >= 200 && $diff <= 299) {
            if ($startPoint <= $enemyPoint) {$gain = -1;}
            else {$gain = -12.5;}
        }
        if ($diff >= 300 && $diff <= 399) {
            if ($startPoint <= $enemyPoint) {$gain = -0.5;}
            else {$gain = -16;}
        }
        if ($diff >= 400 && $diff <= 499) {
            if ($startPoint <= $enemyPoint) {$gain = 0;}
            else {$gain = -20;}
        }
        if ($diff >= 500) {
            if ($startPoint <= $enemyPoint) {$gain = 0;}
            else {$gain = -29;}
        }
    }
     
    $coef = $_POST['coef'];
    $endPoint = $startPoint + ($gain * $coef);
     
    ?>
    <table>
    <tr>
    <td>Points adversaire</td>
    <td>Victoire / défaite</td>
    <td>Gain</td>
    <td>Coef.</td>
    <td>Résultat</td>
    </tr>
    <tr>
    <td><?php echo $_POST['enemyPoint'] ?></td>
    <td><?php echo $_POST['victoire'] ?></td>
    <td><?php echo $gain ?></td>
    <td><?php echo $coef ?></td>
    <td><?php echo $endPoint ?></td>
    </tr>
    </table>

    jusque-là tout fonctionne, sauf si l'adversaire a plus de points que le joueur au départ... ce qui correspond aux "else" dans le code

    je vois vraiment pas où se trouve le problème par contre (il doit bien y en avoir un pourtant Pissed )
    Valter
    Valter
    Admin

    Messages : 66
    Date d'inscription : 18/08/2015
    Age : 30

    Gérer les "if" imbriqués en PHP Empty Re: Gérer les "if" imbriqués en PHP

    Message par Valter Sam 12 Mar - 20:05

    Astuce de pro : ajoute des echo "lol" à toutes tes conditions pour voir où ça plante.
    Locust
    Locust
    Newbie
    Newbie

    Messages : 38
    Date d'inscription : 22/08/2015
    Age : 32
    Localisation : Île de France

    Gérer les "if" imbriqués en PHP Empty Re: Gérer les "if" imbriqués en PHP

    Message par Locust Sam 12 Mar - 20:09

    j'ai essayé ça mais ils ne s'affichent pas pour les "else" Sad
    Kegon
    Kegon
    Newbie
    Newbie

    Messages : 15
    Date d'inscription : 10/03/2016
    Age : 31

    Gérer les "if" imbriqués en PHP Empty Re: Gérer les "if" imbriqués en PHP

    Message par Kegon Sam 12 Mar - 20:11

    C'est pas lisible ta merde.

    Utilise l'opérateur ternaire :

    Code:
    $gain = $startPoint >= $enemyPoint ? 5.5 : 7;
    Locust
    Locust
    Newbie
    Newbie

    Messages : 38
    Date d'inscription : 22/08/2015
    Age : 32
    Localisation : Île de France

    Gérer les "if" imbriqués en PHP Empty Re: Gérer les "if" imbriqués en PHP

    Message par Locust Sam 12 Mar - 20:13

    merci, oui c'est vrai que c'est plus clair

    malheureusement j'ai toujours le même problème avec les "else" ou ":" qui ne sont pas pris en compte

    Code:
    if ($victoire == "oui") {
     
        if ($diff >= 0 && $diff <= 24) {
            $gain = $startPoint >= $enemyPoint ? 6 : 6;
        }
        if ($diff >= 25 && $diff <= 49) {
            $gain = $startPoint >= $enemyPoint ? 5.5 : 7;
        }
        if ($diff >= 50 && $diff <= 99) {
            $gain = $startPoint >= $enemyPoint ? 5 : 8;
        }
        if ($diff >= 100 && $diff <= 149) {
            $gain = $startPoint >= $enemyPoint ? 4 : 10;
        }
        if ($diff >= 150 && $diff <= 199) {
            $gain = $startPoint >= $enemyPoint ? 3 : 13;
        }
        if ($diff >= 200 && $diff <= 299) {
            $gain = $startPoint >= $enemyPoint ? 2 : 17;
        }
        if ($diff >= 300 && $diff <= 399) {
            $gain = $startPoint >= $enemyPoint ? 1 : 22;
        }
        if ($diff >= 400 && $diff <= 499) {
            $gain = $startPoint >= $enemyPoint ? 0.5 : 28;
        }
        if ($diff >= 500) {
            $gain = $startPoint >= $enemyPoint ? 0 : 40;
        }
     
    }
     
       
    elseif ($victoire == "non") {
       
        if ($diff >= 0 && $diff <= 24) {
            $gain = $startPoint <= $enemyPoint ? -5 : -5;
        }
        if ($diff >= 25 && $diff <= 49) {
            $gain = $startPoint <= $enemyPoint ? -4.5 : -6;
        }
        if ($diff >= 50 && $diff <= 99) {
            $gain = $startPoint <= $enemyPoint ? -4 : -7;
        }
        if ($diff >= 100 && $diff <= 149) {
            $gain = $startPoint <= $enemyPoint ? -3 : -8;
        }
        if ($diff >= 150 && $diff <= 199) {
            $gain = $startPoint <= $enemyPoint ? -2 : -10;
        }
        if ($diff >= 200 && $diff <= 299) {
            $gain = $startPoint <= $enemyPoint ? -1 : -12.5;
        }
        if ($diff >= 300 && $diff <= 399) {
            $gain = $startPoint <= $enemyPoint ? -0.5 : -16;
        }
        if ($diff >= 400 && $diff <= 499) {
            $gain = $startPoint <= $enemyPoint ? 0 : -20;
        }
        if ($diff >= 500) {
            $gain = $startPoint <= $enemyPoint ? 0 : -29;
        }
    }
    Valter
    Valter
    Admin

    Messages : 66
    Date d'inscription : 18/08/2015
    Age : 30

    Gérer les "if" imbriqués en PHP Empty Re: Gérer les "if" imbriqués en PHP

    Message par Valter Sam 12 Mar - 20:15

    Indice de pro : Si l'adversaire a plus de points que le joueur, $diff sera forcément...
    Locust
    Locust
    Newbie
    Newbie

    Messages : 38
    Date d'inscription : 22/08/2015
    Age : 32
    Localisation : Île de France

    Gérer les "if" imbriqués en PHP Empty Re: Gérer les "if" imbriqués en PHP

    Message par Locust Sam 12 Mar - 20:23

    Oulala, effectivement...

    j'étais complètement à côté de la plaque. J'avais tellement ce code dans la tête que j'en ai oublié la logique du truc Razz

    Je vais remplacer les if ($diff >= 0 && $diff <= 24)

    par

    if ($diff <= 24)

    Contenu sponsorisé

    Gérer les "if" imbriqués en PHP Empty Re: Gérer les "if" imbriqués en PHP

    Message par Contenu sponsorisé


      La date/heure actuelle est Ven 26 Avr - 17:07