Bank Charges [Resolved]

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Bank Charges [Resolved]

Post 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!";
    }
}

}
?>



Last edited by Epiales on Tue Aug 27, 2013 3:26 am, edited 1 time in total.
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Xaos
Posts: 940
Joined: Wed Jan 11, 2012 4:01 am

Re: Bank Charges

Post 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
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Bank Charges

Post 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']))


Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Bank Charges

Post 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:
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Bank Charges

Post 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
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Xaos
Posts: 940
Joined: Wed Jan 11, 2012 4:01 am

Re: Bank Charges

Post by Xaos »

You can do it that way or *.1 then subtract that. and no problem, glad you finally got the solution !
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Bank Charges

Post 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
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Bank Charges

Post 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 ;)
The indelible lord of tl;dr
Winawer
Posts: 180
Joined: Wed Aug 17, 2011 5:53 am

Re: Bank Charges

Post 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"; 
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Bank Charges

Post 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
The indelible lord of tl;dr
Post Reply

Return to “Beginner Help and Support”