Learn how to create a banking application using Object Oriented Programming that became available in PHP 5.
In our last article, we looked at the code that makes up the home page. We will shortly show a screen of what the home page looks like so that you have a rough idea of what to expect code wise.
The Home page
The home page is the first page that the customer will see after they have been authenticated. It is also responsible for showing the user the following information:
- Account names, most of us have more than one account. The home page will show all accounts that are in the customer's name.
- The balances for those accounts.
- Option to make deposit or a withdrawal
The first part of the code includes all the data that we need to use the classes. We will be using the methods contained in these classes to extract the data that we need:
Also, note that we have started a session because we are going to use the session variables that we created at authentication. We then start to build the HTML page. Up to this point, I've not mentioned the navigation panel:
The navigation panel has three links:
- Logout Â Enables a customer to log out of the application
- View Accounts Â Enables customer to view account details
- Create Account Â This link is actually for administrators of the site rather than a customer, because a customer should only come to the site to view his or her account details.
The following code makes up the links in the navigation panel:
The next few lines of code really do the job of displaying the account details of the customer. The first line instantiates the customer class. Incidentally, the customer class inherits all of the methods of the accounts class. Look at its declaration:
This means that we can use the methods contained in the accounts class as we go
through the scripts, as long as we include this class on our pages, of course.
After instantiating the class, we immediately call the
getaccounts() method. This method extracts
all of the data relating to the customer and displays it for us on the page. It
takes one parameter:
This parameter is the customer ID. It will use this ID to extract account information about the customer. It has the following code:
The method starts by checking to see if the database connection exists. As long as the class is instantiated this connection will almost certainly exist, but still, just to be on the safe side we set this condition:
Now we build the SQL statement that will get the account information that we require:
$sql = "SELECT * FROM accounts WHERE cusid ='".$id."'";
We run the SQL statement with the
Now we test to see if any results have been returned. Obviously if the user exists in the database then there will almost certainly be some information about the user in the database. If the user has no accounts, then the function will crash if we don't make provision for that eventuality:
Once we know that the customer has accounts in the database, we have to retrieve and show them to the customer. The code does just that, it starts by building a table that will host this information:
The first row of the table contains the name of the customer. To get the customer's name, we do not have to use the customer class at all, since the name is stored in a session variable; we simply use it:
The second row is a bit more complex. It needs to show the following:
Type of the accounts i.e Savings, business, etc.
Balance of each account
In addition, it needs to provide the customer with the options to make a withdrawal or to deposit some money into the accounts. So the first thing we need to show is the account's information; this is exactly what the code does next. First the accounts header is built:
Then we run a while loop to extract the customer's account names, account numbers and account balances. Also with each account that is retrieved, the options to withdraw or deposit are also shown. Notice that a hyperlink is created for both the withdrawal and deposit options:
The table is then closed:
The fact that we use a class method(as above) to show the customer information instead of writing it out in the HTML page gives us a clear line of separation between presentation and code, which is often mixed and can cause serious headaches when it comes to debugging code. It also provides a centralized place where you can go to debug problematic code.
Finally we close off the homepage with the following code: