Page 1 of 4

Bank Tutorial

Posted: Sun Oct 18, 2009 6:46 pm
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.

Re: Bank Tutorial

Posted: Sun Oct 18, 2009 7:03 pm
by hallsofvallhalla
nice work!

Re: Bank Tutorial

Posted: Sun Oct 18, 2009 7:09 pm
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

Re: Bank Tutorial

Posted: Sun Oct 18, 2009 7:14 pm
by Ravinos
What errors are you getting?

Re: Bank Tutorial

Posted: Sun Oct 18, 2009 7:52 pm
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'";

Re: Bank Tutorial

Posted: Sun Oct 18, 2009 8:03 pm
by hallsofvallhalla
because he including a session.php, if you do not have session.php then you need to start sessions.

Re: Bank Tutorial

Posted: Sun Oct 18, 2009 8:41 pm
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;
}
?>

Re: Bank Tutorial

Posted: Mon Oct 26, 2009 7:06 pm
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?

Re: Bank Tutorial

Posted: Mon Oct 26, 2009 8:53 pm
by hallsofvallhalla
it means you have another div on top of it. Change the z index of the form. Make it higher

Re: Bank Tutorial

Posted: Tue Oct 27, 2009 10:23 am
by herko125
oh yea...works now like a charm (:
tnx