Building a CMS with PHP and MySQL

NIIT Digital
9 min readJul 8, 2021

--

Photo by Daniel Josef on Unsplash

Originally published at https://www.niit.com/india/

A Content Management System (CMS) is a web application that is utilized to oversee content with tasks such as create, alter and delete content by various users to display to end-users. A CMS commonly has two significant segments: the Content Management Application (CMA), as the front-end UI that permits a client, even with restricted skill, to add, alter, and eliminate content from a site without the intercession of a website admin; and a Content Delivery Application (CDA), that accumulates the substance and updates the site. Many organizations utilize content management systems in their websites to distribute content identified with their business.

To understand the concepts of building software you can check out the Post Graduate Programme in Full Stack Software Engineering at NIIT.

Getting Started: Create the database

The first thing we have to do is to create a MySQL database to store our content.

  1. Open a terminal window and run the MySQL client program:

2. Now create the database

In the mysql> prompt, type:

3. Quit the MySQL client program:

You’ve created a new and empty database in which you can put the database tables and content.

Create an articles database table

Our CMS has just a single database table: articles. This holds all of the articles in the system.

Create a text file tables.sql. A table’s schema depicts the sorts of information that the table can hold, along with the other data about the table.

The code characterizes the schema for the articles table. It’s written in SQL, the language used to create and control information bases in MySQL and some other database systems).

Breakdown of the code:

  • Make the articles table

DROP TABLE IF EXISTS: Articles eliminate any current articles table and information in the event if that already exists. We do this since we can’t characterize a table with a similar name as a current table.

  • Add the publicationDate field

The following line makes the publicationDate field, which stores the date that each article was distributed. This field has a data type of date, which implies it can store “date” values.

  • Add the essential key

The CREATE TABLE characterizes a key for the table. A key can also be called an index, and in basic terms, it makes it faster to discover data in the table, to the detriment of some additional extra room. Therefore, we make the id field a PRIMARY KEY. Each table can have a solitary PRIMARY KEY; this is the key that recognizes each record in the table. Also, by adding this key, MySQL can recover an article dependent on its ID rapidly.

Since we’ve made our table schema, we need to stack it into MySQL to make the actual table. To do this, open up a terminal window and change the folder containing your tables.sql file:

Create a configuration file

Since you’ve made your database, you’ll begin to compose your PHP code.

Create the configuration file to store various settings for the CMS. This file will be utilized by all the script files in the CMS.

In the cms folder, create a file called config.php:

Breakdown of the code:

  • Show errors in the program

The ini_set() line causes mistake messages to be shown in the program. This is useful for troubleshooting. However, it should be set to false on the live site since it tends to be a security hazard.

  • Set the timezone

The CMS will utilize PHP’s date() function, and we need to reveal the PHP server’s timezone.

  • Set the database access subtleties

Now we characterize a steady DB_DSN that reveals to PHP where to discover the MySQL database. Ensure the dbname parameter coordinates with the name of your CMS database. We likewise store the MySQL username and secret key to get to the CMS database in the constants DB_USERNAME and DB_PASSWORD.

  • Set the ways

Set 2-way names in the config document: CLASS_PATH, which is the way to the class files, and TEMPLATE_PATH, which is the place where the content should search for the HTML template files.

  • Set the administrator username and secret key

ADMIN_USERNAME and ADMIN_PASSWORD constants contain the login subtleties for the CMS admin. Once more, you’ll need to change these to your own qualities.

  • Incorporate the Article class

The Article class file is required by all contents in our application, and we need to incorporate it here.

  • Make an exception handler

We characterize handleException(), a basic function to deal with any PHP exemptions raised as the code runs. The function shows a nonexclusive error message and logs the actual exception message to the web server’s error log.

Build the Article class

Now build the Article PHP class as it handles the bare essentials of storing articles in the database and recovering articles from the database. After building this class, it will be simple for our other CMS contents to create, update, recover and delete articles.

In the cms folder, create a classes folder. In that classes folder, create a new file called Article.php:

The front-end index.php script

Now make index.php, the script that controls the display of the front-end pages of the website. Save the file in the cms folder you made before:

The back-end admin.php script

The admin script is a bit more intricate than index.php since it manages all the admin functions for the CMS. The fundamental structure, however, is similar to index.php.

Save the file, admin.php, in a similar folder as your index.php script:

Create the front-end templates

We’ve made all the PHP code for our CMS’s functionality. The subsequent step is to make the HTML templates for both the front-end and admin pages.

  • The include files

Create a folder called template in your cms folder. Now create a folder called include in the templates folder. In this folder, we will put the header and footer markup that is common to each page of the site.

Create a new file called header.php in the include folder:

The code basically shows the markup to begin the HTML page. It utilizes the $results[‘pageTitle’] variable passed from the primary script (index.php or admin.php) to set the title component and furthermore links to a stylesheet, style.css

Now create a file called footer.php in the same folder:

  • homepage.php

Now go to the templates folder and create a homepage.php template file there:

The template shows the article features on the landing page as an unordered list. It loops through the various articles stored in $results[‘articles’] and displays each article’s distribution date, title, and summary. The title is connected back to ‘.’ (index.php), passing action=viewArticle, and the ID of the article, in the URL. This permits the visitor to peruse an article by clicking its title.

  • archive.php

Now create an archive.php template file in the templates folder:

The template displays the archive of the articles in the CMS. It’s practically indistinguishable from homepage.php. It adds the CSS class to the unordered list so we can style the list items a bit diversely to the landing page, and it likewise adds the year to the article distribution dates.

The page incorporates an all-out tally of the articles for the database, recovered by $results[‘totalRows’]. Instead of the archive’s link at the end of the page, it incorporates the “Return to Homepage” link.

  • viewArticle.php

This front-end template shows an article to the user. Now create a file called viewArticle.php in the templates folder.

Back-end templates

We’ve already made the templates for the front end of the site. Now we need to create the three admin templates.

  • loginForm.php

Now create a folder named admin inside the templates folder. In the admin folder, make the first of the three templates, loginForm.php:

  • listArticles.php

Now make the second admin template in the admin folder and name it listArticles.php:

The template shows the list of articles for the admin to alter.After showing any error or status messages, it loops through various article objects stored in $results[‘articles’], showing each article’s distribution date and title in a table row.

  • editArticles.php

Create the final template, editArticles.php, in the admin folder:

The structure incorporates a region for error messages and the fields for the article title, content, summary, and distribution date. At last, there are two buttons for saving and dropping changes and a link to permit the administrator to delete the recently altered article.

Conclusion

A CMS is used for Enterprise Content Management (ECM) and Web Content Management (WCM). ECM generally upholds various users in a collaborative environment by coordinating document management, advanced resource management, and record maintenance.

PHP programming language is one of the solutions for creating sites and online applications. Since PHP cannot store information on its own and depends on a database for storing data safely. MySQL is the best option for PHP designers. As an open-source Relational Database Management System (RDBMS) that utilizes SQL language, MySQL database assists with mechanizing information recovery and offers help in PHP-MySQL web applications development. CMS is pretty standard but gives the starting point to build one’s CMS driven websites using PHP and MySQL. To learn more about various programming languages checkout software developer training courses by NIIT.

--

--

NIIT Digital
NIIT Digital

Written by NIIT Digital

We are a skill development hub for learners worldwide sharing ideas on technology. Explore insightful reads at https://www.niit.com/india/knowledge-center

No responses yet