Photo by on Kat Yukawa Unsplash Introduction This Donations Manager application can be used by charitable organizations to manage the processing of donations as well as ensure accurate data entry and seamless communications with donors. In addition, we will include a donation summary report with charts in our application. Download Complete Source Code System Requirements PHP 7+ MySQL or MariaDB phpGrid phpChart (Part II) Database Overview Our Donation Manager system has four objects. Donor — Personal information about the donor. Donation — The amount donated for a particular campaign Campaign — Donation campaign information Organization — The organization that manages the donations Our database is simple and straightforward. Each table is an entity that represents one of the above objects. Programming Terminology — Object or Entity An entity is a database term that’s typically represented by a table in a database schema. An object is an object-oriental programming term often mapped to a database table. In our data model, is used to store the information about donations; campaigns are used to compile campaign data and information about the campaign; the organization managing the campaign is stored in . donations org Set up the Donation Manager Database Obtain the SQL script from this tutorial’s GitHub repo, and then execute the SQL script using a tool such as MySQL Workbench or Sequel Pro. This will create a new database named and the tables we will need in this tutorial. donation_manager_db.sql MySQL donation_manager Setup phpGrid A datagrid is a spreadsheet-like data table that displays rows and columns which representing records and fields stored in the database table. These grids provide the end-user with the ability to read and write to the database tables from a web page. The datagrid component handles all internal database ( reate, emove, pdate, and elete) operations for us, offering faster and better results with minimal coding. phpGrid CRUD C R U D Be sure to before you proceed. download a free copy of phpGrid To install phpGrid, follow these steps: Unzip the phpGrid download file. Upload the folder to the phpGrid folder. phpGrid Complete the installation by configuring the file. conf.php Before we begin coding, we must include the following information in , the phpGrid configuration file. conf.php UI Design Photo by on Hal Gatewood Unsplash The basic design is simple. It is composed of a single-level top menu bar and a datagrid. In the Reports section, we will add charts using the phpChart. Menus The Donations Manager has four menu items. Each item represents a corresponding table in the Donation Manager database. The include file for the menu is stored in the folder named . The code for the menu is straightforward. For the sake of focus, we will not go into great detail. Feel free to look at the content stored inside the folder. inc menu.php inc We have also added a menu item named which we will address in Part II of this tutorial. Reports Donations Page Our first Donation Manager page is the Donations page on which we display donation information retrieved from the table in a datagrid. Donations Remember that each table in the Donation Manager database has the as the primary key. So, we can easily create our first datagrid with the following two lines of code. id phpGrid 101 — A Basic Datagrid 1. On the first line, you create the phpGrid object to be displayed. 2. Then you call to render the resulting datagrid on the webpage. display() You can learn more about how the basic datagrid works . here Here’s what our datagrid looks like: Donations Now, we will make a few enhancements to the Donations datagrid. First of all, we don’t need to show the primary key as it is only used internally and is meaningless to our users. We can hide it using . Donations id set_col_hidden() Next, the column is a currency value. We can format this column to be displayed as currency using . Amount set_col_currency() We also want our datagrid to display a human-readable Campaign description and Donor name instead of displaying meaningless integers in columns and . We can easily obtain the campaign description and donor name from and table respectively using the function. CampaignId DonorId Campaigns Donors set_col_edittype() We also want to make the Donations grid editable. We can do this by adding the line: The complete code for the Donations datagrid: Here’s how the Donations datagrid looks after our enhancements: Looks good, doesn’t it? Let’s move on! Donors Page Photo by on Akshay Chauhan Unsplash The second page is the Donors page. It contains a list of donors and donation summary information. A relationship exists between the table and because one donor can make one or more donations. The table has a foreign-key field which references the table. one-to-many Donors Donations Donations donorid Donors We can join both tables, and by using an to populate our Donations datagrid. Donors Donations INNER JOIN SQL 101 — INNER JOIN Use an INNER JOIN to create a one-to-many relationship between two database tables. The table on the “one” side of the “one-to-many” relationship should have a primary key column. The other table has a foreign key that points to the primary key in the first table. Here’s our entire SQL statement. To make our data look more interesting, we create a full name by concatenating the first name and last name fields with , a SQL function that concatenates two or more strings. In MySQL, it has the following syntax: FirstName LastName CONCAT To create the donor’s full name, we also add a blank character after FirstName, which adds a space between the first and last names. We also want to display the total amount of donated by a given donor. This is where SQL come in handy. In MySQL, you can use the function to return the total sum of a numeric column. We name our total . aggregation functions SUM() TotalDonation Because we want to display the summary information, we need to add a statement that groups identical data in the result-set. In this case, we assume that if the and fields are them same it means they refer to the same donor. The GROUP BY clause groups records into summary rows for each group. It is typically used with aggregate functions such as COUNT, MAX, MIN, SUM, and AVG. GROUP BY Address Email Putting it all together, here’s the final SQL statement that we will use to create our datagrid. Just in case you were wondering about what the and are for in the SQL statement, they are the SQL table aliases. d dn SQL 101 — Table Alias SQL aliases are temporary names used for table or column. Aliases are often used to make column or table names more readable. It only exists for the duration of the query. Donors Datagrid Below is the code for our datagrid. It is composed of a that uses the above SQL statement and the function. Donors phpGrid constructor display() Our Donations datagrid looks like this: Let’s add a few things to improve its usability. First of all, the first column displays the table’s primary key . We don't need to show this to our users. Let's hide it using the function. id set_col_hidden() Each donor has an email. We can easily make this field an email hyperlink using . set_col_format() The column is a currency field. We can change this column to currency format using . TotalDonation set_col_currency() And both the and fields should be read-only. Name TotalDonation Finally, we make the Donors datagrid editable I(except for the fields we just made read-only) with the function. enable_edit() The complete Donors datagrid code: Here’s how the Donors datagrid looks like after the changes (with edit window open). Donation Detail Grid As one-to-many relationship exists between table and , we can easily present this relationship using the phpGrid feature. Donors Donations master detail Datagrid 101 — Master Detail A one-to-many relationship between two data models can be presented in a master-detail format using datagrids. The detail grid is a regular phpGrid datagrid object and can use the same methods as any other datagrid such as description title, sort, and update etc. The detail grid is dynamically rendered based on the row selected from the parent (master) grid. We will simply reuse the code from datagrid we created earlier in this tutorial. Donations Note that we did NOT include the last line . $dgDonations->display() Finally, we set the master-detail relationship in our code using . The second parameter is the foreign-key defined in the detail table . The third parameter is the foreign-key defined in the master table . set_masterdetails() Donations Donors A series of and how to use them beyond the scope of this tutorial is also available. phpGrid master-detail demos Campaigns Page Photo by on rawpixel Unsplash The Campaigns page contains campaign information and donations details. Note that a given campaign can receive many donations from one or many donors. Again, we can use phpGrid’s feature as we what did on the Donors page. master-detail Let’s start with the Campaigns. Since the table and have a one-to-many relationship, we can join the two tables by creating an . Note that and are the SQL table aliases for the and table respectively. Campaigns Donations INNER JOIN c dn Campaigns Donations We also want to show the total amount that has been donated to a particular campaign. Again, we can use the SQL aggregation function to return the total of the column from the table. SUM() Amount Donations One of the key ways to measure the success of a donation campaign, besides the its total amount of donations received, is the of the donations. Here, we introduce another useful SQL aggregation function called . total number COUNT() The COUNT() function, well, as its name suggests, counts the number of rows returned in a SELECT statement. We give it the name . DonationCount Lastly, since we want to display the campaign summary information, we will add a statement which will group records and print their totals into summary rows for each group. GROUP BY SQL 101 — GROUP BY & Aggregates Use BY whenever you need to use the SQL aggregate functions such as COUNT, MAX, SUM, and AVG. GROUP The complete SQL SELECT statement for our Campaigns datagrid looks like this: Next, we are going to include a few phpGrid functions to jazz up our Campaigns datagrid. First of all, as we did with the others, let’s hide the column . id Then, let’s display our field in a currency format. TotalDonation Don’t forget to make the Campaigns grid editable. Lastly, we add a global search by adding the function which enables a multi-fields global search on any searchable columns. That’s all it takes. enable_global_search() $dgCamp->enable_global_search(true); That’s it for the Campaigns grid. Let’s take a look: The complete code for the Campaigns grid: Donations Detail Grid Remember that we mentioned earlier that the and table have a one-to-many relationship? We can present their relationship using the phpGrid feature similar to the way what we did for the and tables. Campaigns Donations master detail Donors Donations We will simply reuse the same code we used for the datagrid we made earlier in the tutorial. Donations Note that we did NOT include the last line . $dgDonations->display() Finally, we set the master-detail relationship in our code using . Campaigns is the master grid, and Donations is the detail grid. set_masterdetails() Thus, the complete code for the Campaigns page is: This marks the end of the code needed to create the datagrids required for this tutorial. However, we are not done yet. There is still one more page we need to create — Reports. We will cover this section in Part II. Stay tuned! Download Complete Source Code Thanks for reading. If you enjoyed this article, please hit that clap button 👏 to help others find it.