Bank Tutorial

Post all your tuts or request for tuts here.
User avatar
Ravinos
Posts: 281
Joined: Tue Sep 15, 2009 4:14 pm

Bank Tutorial

Post by Ravinos »

This is the bank code from my testing scripts and would like to give it to the community to go over and incorporate into your game. It plugs right in with the standard tutorials. All you have to do is make a bank.php file and a paste the code below into it. This script will post to itself so no need to link it to another page. This can be inserted into a frame or iframe if you would like. It works very well with those.

I am not using the layout in the tutorials however. I am only using the same tables and names. You will have to replace <?php include session.php';?> with the standard session script from the tutorials.

Basics:
The script will allow you to deposit and withdraw gold in the appropriate fields by entering an amount and then hitting withdraw or deposit. It will update on your stat panel in real time if you add the panel to this script. If you do not enter anything into the fields or try to withdraw or deposit an amount of money you don't have it will give you a negative response.

*This is a stripped down basic version of the one I currently have in development. My full version has random responses and replies from the bank teller. Right now he has 4 greetings, 4 deposit, 4 withdraw, and 4 negative responses. The teller's name also changes depending on the location of where you are banking.

** You will also need a field in your players table called 'bank'. this is where the game will store your money that you do not keep on your character.

Code: Select all

<?php
include_once 'connect.php';
session_start();
   ?>
<?php
include 'session.php';?>
<?php
$playerinfo="SELECT * from players where name='$player'";
$playerinfo2=mysql_query($playerinfo) or die("could not get player stats!");
$playerinfo3=mysql_fetch_array($playerinfo2);
?>

<center>
<table width="90%" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <td>Gold:</td>
    <td><?php echo "Your balance is:  " . $playerinfo3['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'] > $playerinfo3['gold'] || $_POST['deposit'] < 0)
	{
		echo "You are trying to deposit too much. Try a smaller amount.";
	}
	else
	{
		$newbankd = ($playerinfo3['bank'] + $_POST['deposit']);
		$newgoldd = ($playerinfo3['gold'] - $_POST['deposit']);		
		$bankdeposit = "update players set bank='$newbankd', gold='$newgoldd' where name='$player'";
		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'] > $playerinfo3['bank'] || $_POST['withdraw'] < 0)
	{
		echo "You do not have enough in your account.";
	}
	else
	{
		$newbankw = ($playerinfo3['bank'] - $_POST['withdraw']);
		$newgoldw = ($playerinfo3['gold'] + $_POST['withdraw']);		
		$bankwithdraw = "update players set bank='$newbankw', gold='$newgoldw' where name='$player'";
		mysql_query($bankwithdraw) or die("could not withdraw bank");		

		echo "You have withdrawn your gold!";
    }
}
?>
Enjoy! I don't think I'll have to make a video to go along with it but if you have any questions please ask.
Last edited by Ravinos on Mon Feb 08, 2010 4:55 pm, edited 1 time in total.
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: Bank Tutorial

Post by hallsofvallhalla »

nice work!
darspire
Posts: 194
Joined: Sat Jul 25, 2009 9:21 pm

Re: Bank Tutorial

Post by darspire »

nice job.
im gettin errors so im gonna have to try to fix them but still i dont know why i didnt think of that
if you love something, let it go.
if it doesnt return.....hunt it down and kill it.
User avatar
Ravinos
Posts: 281
Joined: Tue Sep 15, 2009 4:14 pm

Re: Bank Tutorial

Post by Ravinos »

What errors are you getting?
darspire
Posts: 194
Joined: Sat Jul 25, 2009 9:21 pm

Re: Bank Tutorial

Post by darspire »

i had 3 but i fixed 2 of them. now all thats left is this 1

Notice: Undefined variable: player in C:\wamp\www\tutorial\bank.php on line 6

$playerinfo="SELECT * from players where name='$player'";
if you love something, let it go.
if it doesnt return.....hunt it down and kill it.
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: Bank Tutorial

Post by hallsofvallhalla »

because he including a session.php, if you do not have session.php then you need to start sessions.
User avatar
Ravinos
Posts: 281
Joined: Tue Sep 15, 2009 4:14 pm

Re: Bank Tutorial

Post by Ravinos »

replace my <?php include 'session.php';?>

with:

Code: Select all

<?php

if (isset($_SESSION['player']))
{
  $player=$_SESSION['player'];
}
else
{
  echo "Not Logged in <br><br> <A href='login.php'>Login</a>";
  exit;
}
?>
herko125
Posts: 17
Joined: Fri Oct 16, 2009 3:12 pm

Re: Bank Tutorial

Post by herko125 »

okay....i got a problem
when i try to align this form in center, like the one at store is, but if i do the same way as there i cant write anything in it or click any button

any ideas?
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: Bank Tutorial

Post by hallsofvallhalla »

it means you have another div on top of it. Change the z index of the form. Make it higher
herko125
Posts: 17
Joined: Fri Oct 16, 2009 3:12 pm

Re: Bank Tutorial

Post by herko125 »

oh yea...works now like a charm (:
tnx
Post Reply

Return to “Tutorials”