Creating a Banking Application Admin Panel using Object Oriented Programming [con't]
View accounts script
The view accounts script is shown when the user clicks on the link that is presented on the list customers page. This script shows information about the selected customer's accounts. More specifically, it shows information such as the name on the account, account type (whether it is savings, current or business account), account balance and other relevant information. Below is a screen of the page followed by the code:
View Accounts Code Explained
Now as with most other scripts, this script starts by opening a session and including the classes that are required:
The naviagation panel is built here:
Now we get to the meat of the script. This is where the information about the customer account is retrieved. First, we instantiate the customer class. By instantiating it we copy the methods and attributes of the class to the receiving object, which in this case is the $cust object:
Then we access the getaccounts method by using the -> operator:
By calling the getaccounts() method, we display the customer information that we want. The method takes one parameter, which is the customer's id. This id is then used in an internal database query to retrieve the accounts information that is stored regarding the customer. We will look at this method in detail shortly.
The delete script is responsible for removing customers from the system. It is a very short script. Below is the code listing:
The code below does the job of deleting a customer:
First, we check if the delete attempt was successful and then redirect the user to the list customer page, but only if the delete attempt was successful:
Now let's take a look at the various methods that were used throughout the scripts. Below is a listing of the methods.
We start with the delete customer method. This method has two parts to it. First, it deletes the customer information from the customer table and then it deletes the accounts information from the accounts table. The method takes one parameter, which is the id of the customer. As you can image if you run the delete query without this vital piece of id, the entire contents of your database is going to be deleted. By adding the id, we target a specific customer as opposed to all of the customers:
The getaccounts method retrieves all of the accounts information about a customer, given the customer ID. The method starts off by checking to see if a database connection exists:
Then an SQL statement is created:
Then we run the query:
If the query was successful, then we start to build the table that is going to host the customer information:
Now we come to the main part of the script. This is where the customer information is shown to the user:
With each iteration of the while loop, the customer information is retrieved and displayed:
If the query was not successful then an error message is displayed:
ConclusionObject Oriented Programming in PHP makes compartmentalizing your code extremely easy. If you are working on a large project, it is the recommended way to go. I hope this banking application has at least shown you some of the power that OOP has in PHP.
Original: December 14, 2009