Page 1 of 1

Bank Charges [Resolved]

Posted: Wed Aug 21, 2013 9:16 pm
by Epiales
Hey all,

I've implemented a bank into the game I'm working on, but don't know how to setup a bank fee for deposits. I'm wanting to have a 10% charge on what they put into the bank. Any help would be appreciated. Below is the code:

Code: Select all


<?php
session_start();
include("header.php");

if(!isset($_SESSION['uid'])){
    echo "You must be logged in to view this page!";
}else{ ?>
<center>
<table width="90%" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <td>Gold:</td>
    <td><?php echo "Your balance is:  " . $stats['bank'] . "<br>";?></td>
  </tr>
  <tr>
    <td>Deposit</td>
    <td>Amount:<fieldset>
<form method="post" action="bank.php" target="_self">
<input type="text" name="deposit" value="">
<input type="submit" name="bank_action" value="Deposit">
</form>
</fieldset>
    </td>
  </tr>
  <tr>
    <td>Withrawl</td>
    <td>Amount:<fieldset>
<form method="post" action="bank.php" target="_self">
<input type="text" name="withdraw" value="">
<input type="submit" name="bank_action" value="Withdraw">
</form>
</fieldset>
    </td>
  </tr>
</table>
</center>
</div>
<?php
if (isset($_POST['deposit']))
{
   if ($_POST['deposit'] > $stats['gold'] || $_POST['deposit'] < 0)
   {
      echo "You are trying to deposit too much. Try a smaller amount.";
   }
   else
   {
      $newbankd = ($stats['bank'] + $_POST['deposit']);
      $newgoldd = ($stats['gold'] - $_POST['deposit']);      
      $bankdeposit = "update stats set bank='$newbankd', gold='$newgoldd' WHERE `id`='".$_SESSION['uid']."'";
      mysql_query($bankdeposit) or die("could not update bank");      

      echo "You deposited your gold into the bank!";
   }
}
else if (isset($_POST['withdraw']))
{
   if ($_POST['withdraw'] > $stats['bank'] || $_POST['withdraw'] < 0)
   {
      echo "You do not have enough in your account.";
   }
   else
   {
      $newbankw = ($stats['bank'] - $_POST['withdraw']);
      $newgoldw = ($stats['gold'] + $_POST['withdraw']);      
      $bankwithdraw = "update stats set bank='$newbankw', gold='$newgoldw' WHERE `id`='".$_SESSION['uid']."'";
      mysql_query($bankwithdraw) or die("could not withdraw bank");      

      echo "You have withdrawn your gold!";
    }
}

}
?>




Re: Bank Charges

Posted: Wed Aug 21, 2013 9:18 pm
by Xaos
Something like this


$deposit_amount = $deposit_amount * .1;
You would set up the deposit amount in the form, assign it to that variable, and then do that ^. Then deposit it into the bank

Re: Bank Charges

Posted: Wed Aug 21, 2013 11:37 pm
by Epiales
Okay, I think I'm close, but when I deposit 1000 gold, I end up with only 100 gold in bank. Kind of backwards from the 10% bank fee I'm trying to accomplish LOL.

Code: Select all

    <td>Deposit</td>
    <td>Amount:<fieldset>
<form method="post" action="bank.php" target="_self">
<input type="text" name="deposit" value="">
<input type="hidden" name="deposit_amount" value=".1" />
<input type="submit" name="bank_action" value="Deposit">
</form>
</fieldset>
    </td>
  </tr>
  <tr>
    <td>Withrawl</td>
    <td>Amount:<fieldset>
<form method="post" action="bank.php" target="_self">
<input type="text" name="withdraw" value="">
<input type="submit" name="bank_action" value="Withdraw">
</form>
</fieldset>
    </td>
  </tr>
</table>
</center>
</div>
<?php
if (isset($_POST['deposit']))
{
   if ($_POST['deposit'] > $stats['gold'] || $_POST['deposit'] < 0)
   {
      echo "You are trying to deposit too much. Try a smaller amount.";
   }
   else
   {
      $newbankd = ($stats['bank'] + $_POST['deposit']);
      $newgoldd = ($stats['gold'] - $_POST['deposit']);
	  $deposit_amount = $_POST['deposit'] * $_POST['deposit_amount'];      
      $bankdeposit = "update stats set bank='$deposit_amount', gold='$newgoldd' WHERE `id`='".$_SESSION['uid']."'";
      mysql_query($bankdeposit) or die("could not update bank");      

      echo "You deposited your gold into the bank!";
   }
}
else if (isset($_POST['withdraw']))



Re: Bank Charges

Posted: Wed Aug 21, 2013 11:42 pm
by Epiales
Okay, if I change my .1 to .9 I think it might work out?

So I deposit 1200 gold and bank has 10800 gold in it... :D Thanks for pointing me in the right direction Xaos :mrgreen:

Re: Bank Charges

Posted: Wed Aug 21, 2013 11:50 pm
by Epiales
Well no! grrrrr

It works the first time I deposit, but the next time I deposit, it removes the first deposit and only does the 10% for the new deposit; removing the gold that was in there LMAO. Grrrr


*EDITED AGAIN* Okay, so I changed this line:

Code: Select all

	  $deposit_amount = $_POST['deposit'] * $_POST['deposit_amount'] + $stats['bank'];      
All seems to work well now. YAY

Re: Bank Charges

Posted: Thu Aug 22, 2013 12:32 am
by Xaos
You can do it that way or *.1 then subtract that. and no problem, glad you finally got the solution !

Re: Bank Charges

Posted: Thu Aug 22, 2013 12:41 am
by Epiales
Xaos wrote:You can do it that way or *.1 then subtract that. and no problem, glad you finally got the solution !
Thank you! And again, thanks for pointing me in the right direction. :) Love this place! I'm sure I'll be busy here for awhile. Now I need to go back to adding a timer for training troops, or something else. I did look around for the timer, but got me brain frizzeled, so I took a break lol. I may just try and implement some HP on top of what I have thus far.

Right now I have where you buy henchmen to help increase your attack and guards to help increase your defense. Then they each will have weapons to help boost one another. Then we produce gold per turn so that we can buy the weapons and such. Then we can attack other players and have two choices to choose from on what to attack them for. Gold for buying weapons and something else for buying the henchmen.

I haven't included any HP yet, so thought that might be my next chore. As you can see from the code, it's a bit different than the code in the tutorials, but I think I can make it work. If I do run across anything, I'll be sure to ask. Got tons to do and more to implement. Should be fun by the time I'm through with it. Thanks again for your help.

X

Re: Bank Charges

Posted: Thu Aug 22, 2013 5:06 am
by Jackolantern
As a quick tidbit that is incredibly useful, any C-style programmer should learn:

Code: Select all

$stats['bank'] += $_POST['deposit_amount'] * 0.9;
I am not sure if that is right, as I am not sure what the difference between deposit and deposit_amount is, but I am wanting to show the += operator. It works on numbers, for example:

Code: Select all

$a = $a + $b;
...can be rewritten as:

Code: Select all

$a += $b;
And it works for strings too:

Code: Select all

$greet = "Hello";
$greet += " world";
It seems like a simple shortcut, and in many cases it is, but it can also dramatically make complex assignment operations much easier to read ;)

Re: Bank Charges

Posted: Thu Aug 22, 2013 5:39 am
by Winawer
Jackolantern wrote: And it works for strings too:

Code: Select all

$greet = "Hello";
$greet += " world"; 
You mean (since it's php)

Code: Select all

$greet = "Hello";
$greet .= " world"; 

Re: Bank Charges

Posted: Thu Aug 22, 2013 5:45 am
by Jackolantern
Winawer wrote:
Jackolantern wrote: And it works for strings too:

Code: Select all

$greet = "Hello";
$greet += " world";
You mean (since it's php)

Code: Select all

$greet = "Hello";
$greet .= " world";
Ohh, haha, yeah I forgot about that (it has been a long time since I used PHP). The dot operator instead of + for concatenation always irritated me lol :P