I am creating my purchasing system for my game at the moment, but I am struggling on a part of it and wondered if any of you could offer me some guidance?

The problem I am facing is adding X amount onto a value but if that means it goes over Y then only add up to Y. The player shall purchase a piece of food that shall raise their health by 20 (with 100 being full health), how do I make it so that if the current health value is 90 then it won't go to 110 e.t.c.

So far I have:



    mysql_connect ("localhost", "root", "") or die ("Couldn't Connect to Server");
    mysql_select_db ("Database") or die ("Couldn't Find Database");

    $Purchase = mysql_real_escape_string($_POST['Purchase']);
    $Username = $_SESSION['Username'];

    $Money = mysql_query ("SELECT * FROM Character_Data WHERE Username = $Username");
    while ($Results = mysql_fetch_array($Money))

        /* Purchase of Meals */

        if($Purchase = "Peasant")
            if($Results['Silver'] >= 20)
                $Withdraw = $Results['Silver'];
                $Withdraw = $Withdraw - '20';

                $Stamina = $Results['Stamina'];

                /* HELP - ADD TO STAMINA BUT NO FURTHER THAN 100 */

                $Health = $Results['Health'];

                /* HELP - ADD TO HEALTH BUT NO FURTHER THAN 100 */

                mysql_query ("UPDATE Character_Data SET Silver = $Withdraw WHERE Username =$Username");

            die (Header ('Location: ../Errors/Money.php'));



I hope you understand and that you shall be able to understand me. I don't want the player to be able to gain more than 100% health or stamina through eating.

(I haven't tested the above code so it might spit out some errors)

Thank you

You could add something simple like this following after increasing the health value.

$health = min($health, 100);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.