简体   繁体   中英

Linking a mysql result to another

Sorry for the confusing question, I'm a beginner looking for some improvements to my code.

What I'm currently trying to do is "Linking" a mysql value to another. Here's an example :

  • I'm creating a PHP-based fight
  • There is a database named "fightm" which contains important informations
  • Some of the informations are the ATTACKS and the COOLDOWN
  • I want to simplify my code by having ATTACK and COOLDOWN linked together

To clarify, I'll give you some code (Don't forget that I'm a beginner, if this is bad written you can correct me)

        if (isset($_POST['fdd_attackbas1']) AND ($infomonstre["fightm_cdbas1"] < 1)) {
            $query = $db->prepare('UPDATE users SET vieac=(vieac - :damage ) WHERE username=:username');
            $query->bindValue(':username', $username, PDO::PARAM_INT);
            $query->bindValue(':damage', $damage, PDO::PARAM_INT);
            $query->execute();
            $query->CloseCursor();
            $query = $db->prepare('UPDATE fightmonster SET 
            fightm_life=(fightm_life - :atqb),
            fightm_dmgperso=:atqb,  
            fightm_dmgenemy=:damage, 
            fightm_cdfuite=GREATEST(0, fightm_cdfuite - 1),           
            fightm_cdbas1=(fightm_cdbas1 + 1), 
            fightm_cdbas2=GREATEST(0, fightm_cdbas2 - 1), 
            fightm_cdbas3=GREATEST(0, fightm_cdbas3 - 1), 
            fightm_cdrare1=GREATEST(0, fightm_cdrare1 - 1), 
            fightm_cdrare2=GREATEST(0, fightm_cdrare2 - 1), 
            fightm_cdultime=GREATEST(0, fightm_cdultime - 1)
            WHERE player_id =:id');
            $query->bindParam(':id', $donnees['id'], PDO::PARAM_INT);
            $query->bindValue(':damage', $degatreduiteffet, PDO::PARAM_INT);
            $query->bindValue(':atqb', $atkboosteffet, PDO::PARAM_INT);
            $query->execute();
            $query->CloseCursor();
            header("Refresh:0");
            exit();
    }

                if (isset($_POST['fdd_attackbas2']) AND ($infomonstre["fightm_cdbas2"] < 1)) {
            $query = $db->prepare('UPDATE users SET vieac=(vieac - :damage ) WHERE username=:username');
            $query->bindValue(':username', $username, PDO::PARAM_INT);
            $query->bindValue(':damage', $damage, PDO::PARAM_INT);
            $query->execute();
            $query->CloseCursor();
            $query = $db->prepare('UPDATE fightmonster SET 
            fightm_life=(fightm_life - :atqb),
            fightm_dmgperso=:atqb,  
            fightm_dmgenemy=:damage, 
            fightm_cdfuite=GREATEST(0, fightm_cdfuite - 1),           
            fightm_cdbas1=GREATEST(0, fightm_cdbas1 - 1),
            fightm_cdbas2=(fightm_cdbas2 + 1), 
            fightm_cdbas3=GREATEST(0, fightm_cdbas3 - 1), 
            fightm_cdrare1=GREATEST(0, fightm_cdrare1 - 1), 
            fightm_cdrare2=GREATEST(0, fightm_cdrare2 - 1), 
            fightm_cdultime=GREATEST(0, fightm_cdultime - 1)
            WHERE player_id =:id');
            $query->bindParam(':id', $donnees['id'], PDO::PARAM_INT);
            $query->bindValue(':damage', $degatreduiteffet, PDO::PARAM_INT);
            $query->bindValue(':atqb', $atkboosteffet, PDO::PARAM_INT);
            $query->execute();
            $query->CloseCursor();
            header("Refresh:0");
            exit();
    }

As you can see, i'm repeating myself for each "attack". Now, imagine that I have more than 20 attacks ! That would be insane.

What i would like to do is something this way (THE CODE I WANT BUT WITH COMMENTS FOR THE CODE PARTS I DON'T KNOW)

        if (isset(//One of the attack//) AND (// The CD linked to the attack //)) {
        $query = $db->prepare('UPDATE users SET vieac=(vieac - :damage ) WHERE username=:username');
        $query->bindValue(':username', $username, PDO::PARAM_INT);
        $query->bindValue(':damage', $damage, PDO::PARAM_INT);
        $query->execute();
        $query->CloseCursor();
        $query = $db->prepare('UPDATE fightmonster SET 
        fightm_life=(fightm_life - :atqb),
        fightm_dmgperso=:atqb,  
        fightm_dmgenemy=:damage, 
        // COOLDOWN PART, SAME BUT WITHOUT NEEDING TO CHANGE //
        WHERE player_id =:id');
        $query->bindParam(':id', $donnees['id'], PDO::PARAM_INT);
        $query->bindValue(':damage', $degatreduiteffet, PDO::PARAM_INT);
        $query->bindValue(':atqb', $atkboosteffet, PDO::PARAM_INT);
        $query->execute();
        $query->CloseCursor();
        header("Refresh:0");
        exit();
}

This would be very helpful, but I can't understand how it can be done. Thanks you and sorry for the long text !

Restructure your code into a foreach loop like this:

$keys = ['fdd_attackbas1', 'fdd_attackbas2', ..];
foreach ($keys as $key) {
  if (isset[$key] && ..) {
    ..
    exit();
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM