Revisiting my first PHP app after a month and its improvements...
Tags: webdev, php
Published: 2023-02-13 09:17:09

Last month (7th January 2023) I had posted an article on this site, [Notes about my first PHP app](https://dev.to/prsaya/notes-about-my-first-php-app-2297). It was a quiz app. It was part of my learning PHP.

The present post is about writing similar app. The app's functionality is same. But, this time I had coded using some newer PHP features. Notable among them are the usage of data from a database. Another feature is the usage of a layout template with content from different actions. Finally, the app has a better look.

---

## The Data

The quiz data is now stored in a database. The MySQL Server has a `quiz` database with two tables - `data` and `options`.

The data is created using SQL scripts. The app connects to the database and reads data from the tables using `mysqli` APIs. You will notice that the data is normalized.

The previous app's data was JSON documents stored in a file. Each JSON document represented a quiz item and had composite data types like arrays.

---

## The App

The app has following components:

* `DatabaseConnection`: This class extends `mysqli` class to create a database connection object.
* `DatabaseTable`:  An instance of this class is created for each table using the connection object. This has methods like, `findAll`, `findById`, `findByColumn`, etc., to access the database data.
* `Controller`: This class takes all the `DatabaseTable` objects as input. An instance starts or uses an existing session. This has methods for each of the app's actions (start, play, and end). Each method returns the view template and the data to be rendered in it.
* `index`: This is the entry point to the app. This is also the entry point to each of the app's functions. This creates the database connection, the table and controller objects for each action. Based upon the action, specific controller method is executed. The output is rendered into the app's main layout template.
* `home_html`, `play_html`, `end_html` are the views for the start, the quiz play and the quiz end pages respectively. The `layout_html` is the main template.
* `styles.css` is for better looks (or style) and `onload_js.js` is JavaScript to set a button's state. `play_js.js` has a JavaScript function that evaluates if a question is answered correctly and then makes an Ajax call to `score.php` to update the score (which is tracked in a session variable).

---

## App's Screenshots:

Here are two screenshots of the app running in a browser - the landing and the quiz play pages.


![Quiz App's home page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5paqgkbs09u6d6gpdesk.png)


![Quiz App's play page with quiz question and options](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ev105lp6rkkmlkabf3q2.png)

---

## Conclusion

This time in addition to object orientated classes, arrays (and its various functions like shuffle, extract, in_array) and get/post/session variables, notable PHP features used are:

* [spl_autoload_register](https://www.php.net/manual/en/function.spl-autoload-register.php) for registering given function as `__autoload()` implementation.
* [Output Buffering Control](https://www.php.net/manual/en/book.outcontrol.php) functions `ob_start` and `ob_get_clean`.
* [MySQL Improved Extension](https://www.php.net/manual/en/book.mysqli.php) with its object orientated APIs to connect to the database and read from the tables.

The app has a better structure and classes in reusable formats. These can be used in other applications with minor modifications. The app is structured to have more tables and more functionality. This is work in progress. The app has better looks due to improved CSS.

The GitHub repository [An Example PHP App](https://github.com/prsaya/an-example-php-app.git) has the source code. It also has details on MySQL Server 8 installation, `mysqli` extension configuration, and the SQL scripts for creating the database with sample data. The app is developed using PHP 8.2 and the database is MySQL 5.5.

---