Introduction

In the inaugural post of this series, I introduced you to the Course Projects Manager, a tool designed to manage academic projects. Today, we delve into the core technologies that make this application tick: PHP and MySQL. These technologies work together to power the backend of our web application, handling everything from data storage and retrieval to user interactions.

PHP: The Server-Side Powerhouse

At the heart of the Course Projects Manager is PHP, a robust server-side scripting language. PHP breathes life into our application, enabling dynamic content rendering based on user interactions and database communications. Its role is crucial in processing the logic behind user requests, such as listing, adding, and deleting courses or projects.

PHP's logic is fairly intuitive. Consider the scenario where a user decides to add a new course. The code snippet below showcases the PHP backend in action. Processing the add course request using a public function, It validates the user input. Crucially, this process includes retrieving the user's ID from the session, which is an essential step for associating the course with the correct user account.

Upon successful validation, and here's where the power of PHP8s' object-oriented programming (OOP) capability shines, the process leverages an instance of the Course class. This OOP approach not only facilitates a more organized and modular code structure but also simplifies the interaction with the MySQL database to log the new course details. Finally, with the header function it basically tells the browser where to go to determine what to display to the user once a course is added.

            
    public function addCourse() {
        $userID = $_SESSION['userID'];
        $courseName = isset($_POST['courseName']) ? $_POST['courseName'] : '';
        
        if (!empty($courseName) && isset($userID)) {
            Course::add_course($courseName, $userID);
            header("Location: .?action=list_courses");
            exit;
        } else {
            $error = "Invalid course data.";
            include 'views/error.php';
            exit();
        }
    }
            
          

MySQL: a Relational Data Structure

MySQL plays an indispensable role as the relational database management system for our project. It stores all the crucial data, from user credentials to detailed information about projects. Expectedly, each courses would have its own projects and each user would have their own courses and projects. MySQL's reliability and efficiency in handling this kind of relational data scenario shine through when using foreign keys. I created 3 tables to organize data: a users table, a courses table, and a project table. In picture below, you can see the structure of each table.

Entity Relationship (ER) Diagram for Course Projects Manager using phpMyAdmin's Designer feature

Using phpMyAdmin, a web-based application for graphically managing MySQL databases, more specifically using Designer feature of phpMyAdmin, the relationship between the 3 different tables can be highlighted through an Entity Relationship (ER) Diagram.

In the diagram, the users table is connected to both the courses and projects tables via foreign key relationships. These connections are represented by lines indicating that each course and project is associated with a user, hence enforcing referential integrity within the database. The users table has a primary key called userID, which is used as a foreign key in the courses and projects tables, linking courses and projects to the users who created them. Similarly, The courses tables has a primary key called courseID which is used as a foreign key in the projects tables

MVC: a Scalable and Modular Structure

A Challenging Concept at First

I wasn't sure what Models, Views and Controllers are or how they really work together. I had to do research on the matter. One thing that I think clarified things a little for me was seeing a codebase structure for an MVC project. Here is a picture outlining the Course Projects Manager codebase structure.

Codebase structure for Course Projects Manager outlining the folder and file placement

A Better Understanding

The Model-View-Controller (MVC) structure successfully achieves scalability and modularity: key aspects of today's industry. This design pattern compartmentalizes the application logic (Model), the user interface (View), and the control logic (Controller), making the application more organized, easier to manage, and adaptable.

For example, thanks to this modularity, a Model can be adapted to interface with a different database or data source without having to alter the views and controllers. Thanks to the scalability characteristic, a new feature can be introduced without changing much the pre-existing code just like I added the user authentication feature after finishing with the CRUD operations relating to courses and projects. I just had to add controllers, views and models for users without disrupting the rest of the components. Each request is routed through a controller that interacts with the models to access and manipulate data, often interacting with a database, and then dynamically generates the user interface via the views.

Important to Notice Here!

Models and views do not directly communicate each other in an MVC framework! Instead Controllers organize their interactions. Below is an example illustrating this process. In the coursesController, the flow begins with the retrieval of the user's ID from the session. The controller then invokes the model to fetch the relevant courses and passes this data to the view for presentation.

            
    public function listCourses() {
        $userID = $_SESSION['userID'];
        $courses = Course::get_courses($userID);
        include 'views/courses/index.php';
    }
            
          

Index.php: a Main Router

It's a common practice to turn over the main routing logic to an index.php file which would serve as a central router and an entry point to the application initializing sessions and directing traffic to sub controllers based on user's actions. One way to do that is through PHP's switch statement specifying the different potential cases for a given action. Below is a snippet of that switch statement directing different action cases such as logging out to their corresponding sub controller functions.

            
    switch ($action) {
        case 'login':
            $loginController->showLoginForm();
            break;
        case 'handle_login':
            if ($isPost) {
            $loginController->handleLogin();
            }
            break;
        case 'logout':
            if ($isPost) {
            $logoutController->logout();
            }
            break;
    }
            
          

PHP and MySQL: A synergetic relationship

The integration between PHP and MySQL is demonstrated in features like the course management system, where PHP scripts execute MySQL queries to fetch, insert, or update data within the database. This synergy allows for a dynamic and responsive user experience as the data on the web page reflects the real-time state of the database like we can on the picture below.

Course list feature from Course Projects Manager

Stay Connected

I hope this post sheds light on the power of PHP and MySQL in web development. If you're interested in seeing the full code, check out my course-projects-manager repository on Github.

See you in part 3!