Today I will try to explain how to train and untrain units, which user will use to attack others or defend himself.
Theory is simple:
- each unit cost 100 money
- if you untrain unit it refunds you with 50 money
//we need 2 form parts (html)
<form>
Number: <input size="3" maxlength="4" value="0" name="train_s">
<input name="train" type="submit" value="Train">
</form>
<form>
Number: <input size="3" maxlength="4" value="0" name="untrain_s">
<input name="untrain" type="submit" value="Untrain">
</form>
//now for the PHP part
//first if train button is pressed
if ($_POST[train]) {
$train_s = strip_tags(addslashes($_POST[train_s])); //number of soldiers trained
//we check if it is numeric and positive
if (!is_numeric($train_s)) { die("Wrong input!"); }
if ($train_s < 0) { die("Wrong input!"); }
//we check if we have enough money
$train_money = $train_s * 100;
if ($user[money] < $train_money) { die("Not enough money to train so many units!"); }
//if script got it to this point it was all ok so we can update
mysql_query("UPDATE users SET money=money-'$train_money', units=units+'$train_s' WHERE id='$user[id]'");
}
//we now make script which will run if we press Untrain button
if ($_POST[untrain]) {
$untrain_s = strip_tags(addslashes($_POST[untrain_s])); //number of soldiers trained
//we check if it is numeric and positive
if (!is_numeric($untrain_s)) { die("Wrong input!"); }
if ($untrain_s < 0) { die("Wrong input!"); }
//we check if user has enough units to untrain
if ($user[units] < $untrain_s) { die("You cannot untrain so many units!"); }
//if script got it to this point it was all ok so we can update, but first calculate amount of gained money
$gain_money = $untrain_s * 50;
//we update, so we add money gained because of untraining units, and change number of units
mysql_query("UPDATE users SET money=money+'$gain_money', units=units-'$train_s' WHERE id='$user[id]'");
}
This is all for now. Soon we will create script that would allow us to buy weapons.
Bye - until next time ;)
2 comments:
Post a Comment