Mysidia Adoptables Support Forum

Mysidia Adoptables Support Forum (http://www.mysidiaadoptables.com/forum/index.php)
-   Tutorials and Tips (http://www.mysidiaadoptables.com/forum/forumdisplay.php?f=27)
-   -   (1.3.4) Banks (http://www.mysidiaadoptables.com/forum/showthread.php?t=5285)

Dinocanid 12-05-2016 09:25 PM

(1.3.4) Banks
 
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 Code:

<?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 Code:

 <?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 Code:

public $bank

Afterwards, add this with the rest of the functions:
PHP Code:

  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 Code:

public $bank

And this with the other functions:
PHP Code:

      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.

Corsair 12-06-2016 03:10 PM

Thank you for sharing! I tried it and it works perfectly.

parayna 12-06-2016 05:02 PM

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

Dinocanid 12-06-2016 05:18 PM

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.

parayna 12-06-2016 05:51 PM

Ok! ^_^ I have to say the bank will be such a great feature T-T Thanks XD So helpful to have XD

Abronsyth 12-07-2016 07:22 AM

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 Code:

<br><a href='{$path}bank'>Return to Bank</a

Works splendidly, thank you for sharing!

Hall of Famer 12-07-2016 08:15 AM

Looks nice, congratulations. You may want to make this into a Mod/Plugin, if you can.

Dinocanid 12-07-2016 12:15 PM

@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.

Hall of Famer 12-07-2016 01:40 PM

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.

Ittermat 12-09-2016 08:13 PM

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


All times are GMT -5. The time now is 11:40 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.