(1.3.4) Banks

Forum
Last Post
Threads / Messages

Dinocanid

Member
Member
Joined
Aug 30, 2016
Messages
520
Points
18
Age
23
Location
Maryland, USA
Mysidian Dollar
43,334
After over an hour of work and smashing the keyboard wondering why the forms weren't working correctly, I present to you:
banks!

-Step 0-
Go to phpMyAdmin, adopts_users, and create a new column with this info:
Code:
Name: bank
Type: int
Length/Values: 11
Default: As defined 0
check the null box

-Step 1-
Create a new page for your bank from scratch. If you don't know how to make pages from scratch go here. Do not create a new page from the admincp.
Create a file named bank.php in your root folder and add the following contents:
PHP:
<?php

class BankController extends AppController{

    public function __construct(){
        parent::__construct("member");	
    }
	
	public function index(){
		$mysidia = Registry::get("mysidia");
	}
}
?>

Next, make a new file called bankview.php in your view folder with the following contents:
PHP:
 <?php
class BankView extends View{

    public function index(){
      
        $mysidia = Registry::get("mysidia");
        $document = $this->document;        
        $document->setTitle("The Bank");  
        $balance = $mysidia->user->getbank();
        
        if ($balance <= 0){
        $document->add(new Comment("<h2>Current Balance: 0 CURRENCY</h2>"));
        }
        else $document->add(new Comment("<h2>Current Balance: {$balance} CURRENCY</h2>", FALSE));
        $document->add(new paragraph);

if($mysidia->input->post("deposit")){
$amount = $mysidia->input->post("amount");
$balance = $mysidia->db->select("users", array("bank"))->fetchColumn();
$mysidia->user->changecash(-$amount);
$mysidia->user->changebank(+$amount);
$document->add(new Comment("<h2>You deposited {$amount} CURRENCY into your bank account</h2>", FALSE));
$document->add(new Comment("<br><a href='{$path}bank'>Return to Bank</a>  ", FALSE));
return TRUE;}

$depositForm = new FormBuilder("depositForm", "", "post");
$depositForm->buildComment("Amount: ", FALSE)
  ->buildTextField("amount", FALSE)
  ->buildButton("Deposit", "deposit", "submit");
        $document->add($depositForm);
        
        
        
        
     if($mysidia->input->post("withdraw")){
$amount = $mysidia->input->post("amount");
$balance = $mysidia->db->select("users", array("bank"))->fetchColumn();
$mysidia->user->changecash(+$amount);
$mysidia->user->changebank(-$amount);
$document->add(new Comment("<h2>You withdrew $ {$amount} from your bank account</h2>", FALSE));
$document->add(new Comment("<br><a href='{$path}bank'>Return to Bank</a>  ", FALSE));
return TRUE;}  

 $withdrawForm = new FormBuilder("withdrawForm", "", "post");
$withdrawForm->buildComment("Amount: ", FALSE)
  ->buildTextField("amount", FALSE)
  ->buildButton("Withdraw", "withdraw", "submit");
        $document->add($withdrawForm);
    }
}
?>

-Step 2-
Now go to class_member.php and add this with the other public items:
PHP:
public $bank;

Afterwards, add this with the rest of the functions:
PHP:
  public function changebank($amount){     
	  $mysidia = Registry::get("mysidia");
      if(!is_numeric($amount)) throw new Exception('Cannot change user money by a non-numeric value!');
	  
	  $this->bank += $amount;    
	  if($this->bank >= 0){ 
		 $mysidia->db->update("users", array("bank" => $this->bank), "username = '{$this->username}'");
		 return TRUE;		  	
	  }
	  ///else throw new InvalidActionException("It seems that {$this->username} cannot afford this transaction.");
  }
(The else function is commented out for now so the user avoids any errors. I'll clean it up when I find a way)

Next, go to class_user.php and add this with the other public things:
PHP:
public $bank;

And this with the other functions:
PHP:
      public function getbank(){
     return $this->bank;
}

-End-
That should be it. Now users are able to add and withdraw from a bank account that holds their currency. As of right now, the user's money doesn't appear to go down until they go to another page and the balance doesn't appear to go up until they leave the page and come back. If anyone knows how to make it update right then and there, let me know!
Be sure to let your users know that if they refresh the bank page after depositing money then it will deposit the same amount again. The same goes for withdrawing money. A "confirm resubmission" popup appears on some browsers though (including chrome), which should help avoid accidents.
 
Last edited:
Ooh, this is interesting! Technically couldn't you make it so that after the withdraw or deposit it forwards you automatically to abother page that says a message? (Such as "Thank you for your deposit, we'll take good care of your *currency*!" Maybe a different page for withdrawing.) Then you'd see the currency update and also avoid refreshes. You could also include a "Return to bank" option so people can still do things in it. The button would just link you to the bank again instead of back a page. Just my thoughts ^_^ This would mean extra pages. .. but to avoid users potentially complaining I'd put up with that XD
 
That does sound like it would cause less problems in the long run xD
It wouldn't take me very long to add either since I have much of the base done. I'll probably work on it tonight.
 
Ok! ^_^ I have to say the bank will be such a great feature T-T Thanks XD So helpful to have XD
 
You've actually set it up perfectly to add a little back button that will bring you back to the bank without refreshing. Just add this under comments for withdrawing and depositing:
PHP:
<br><a href='{$path}bank'>Return to Bank</a>

Works splendidly, thank you for sharing!
 
@HOF: Would I just post the updated version in the mods section? I'm not entirely sure how plugins work but I see a spot for it in the adminCP. Is it related to that?
@Abrosynth: It works perfectly, thanks! I'll add it in.
 
Yeah it will be good this way. If possible, upload modified files as attachment in your thread so users with fresh installation of Mysidia can just download/upload easily.
 
EDIT: NVM I was a ***** and forgot the table step...its been a long day



So I followed the instructions- but when I go to deposit money I get this error..

Fatal error: Uncaught exception 'Exception' with message 'Database error 1054 - Unknown column 'bank' in 'field list'' in /home/atrocity/public_html/classes/class_database.php:213 Stack trace: #0 /home/atrocity/public_html/classes/class_database.php(142): Database->_query('users', Array, 'select', NULL) #1 /home/atrocity/public_html/view/bankview.php(19): Database->select('users', Array) #2 /home/atrocity/public_html/classes/class_frontcontroller.php(100): BankView->index() #3 /home/atrocity/public_html/index.php(78): FrontController->render() #4 /home/atrocity/public_html/index.php(82): IndexController::main() #5 {main} thrown in /home/atrocity/public_html/classes/class_database.php on line 213
 
Last edited:
I love this! Is there a way to allow players to accrue interest by visiting this page once a day or so? With a button click, preferably. Thanks so much for sharing this with us!
 

Users who are viewing this thread

  • Forum Contains New Posts
  • Forum Contains No New Posts

Forum statistics

Threads
4,277
Messages
33,120
Members
1,602
Latest member
BerrieMilk
BETA

Latest Threads

Top