You can display star rating which is stored as integer (1 to 5) in database, in an html table using PHP and MySQL. For example, if you want to show a list of hotels with thier star ratings ( from 1 star to 5 star), you can develop an application in PHP to display them in a html table.
In this topic, we will create this simple application to display star ratings using PHP and MySQL.
We will take a simple example of a database table that stores hotel details. Each hotel has a rating field which is a number and can have values between 0 and 5. We have added '0' also for not rated hotel. Each number has caption, 0 - Not Rated, 1- One Star, 2 - Two Star , 3 - Three Star, 4 - Four Star, 5 - Five Star.
We will display the details of the hotels in a html table and in this table, there will be one column for rating. In the rating field, we will display the star symbols and the description or caption for each rating with color as shown below.
Folders and Files
Below is a screenshot of the folder structure and files used:
I have created a folder called 'star_rating' under 'xampp/htdocs' as I am using XAMPP. If you are using WAMP, create 'star_rating' folder under 'www'.
Folder 'cfg' - In this folder I have kept
dbconnect.php which is used to connect to the MySQL database.
Folder 'css' - Custom stylesheet is in this folder
index.php is the main program that displays a list of hotels with star rating.
Below is the screenshot of the page:
Step 1 - Create a database table for hotels
Let us create a table named 'hotels' in MySQL database. I have a database called 'demo'. So 'hotels' Table will be created in this database. If you have an existing database other than demo you can also use it. Just make sure same table name does not exist already. Table structure is given below:
Table has 5 columns.
- id - It is the primary key and auto incremented
- hotel_name - Name of the hotel
- description - Description of the hotel
- rating - Rating of the hotel, it is an integer, values 0 - 5
- date_modified - Date of modification, default current date
Create table script for this table is given below, you can use this code to create the table. You can also download the scripts from the download section later in this topic.
After you run the scripts, verify if data is inserted correctly, it should have below data in it:
Step 2 - Connect to MySQL database (dbconnect.php)
Use below script to connect to the database. Note that we have this database connection php program in 'cfg' folder. This is written once and used in every program where database connection is needed. This will be easy for maintenance and also will enable reusability of the code.
I am using
mysqli_connect() function which needs 4 parameters.
- server - in our case it is localhost
- userid - we are using root user
- password - no password for user root
- database name - demo in our case.
For detail database connection understanding please read topic How to Connect to MySQL database using PHP.
Step 3 - Write a PHP program to display hotels with star ratings (index.php)
Rating for each hotel will be displayed as star symbol and depending on the rating, one or more star symbols will be highlighted. Also, a description of the rating will be displayed with color code, like for One Star it will be red and for Five Star color will be green. If no rating is given, default rating will be zero and it will denote Not-Rated as shown below:
Let us now see the code of
We have a simple html table with three fields. For rating, we have a number, but we need to display stars and highlight number of stars based on the rating value.
Using a for loop, star symbols are displayed. All stars with values less than or equal to the rating are being added with a class called 'selected'. This class is defined in the
style.css and it is used to show stars with highlighted color.
Step 4 - Add CSS (style.css)
Let us add below styles. I have already added
You can see class 'selected' is added along with other simple styles. Keep this
style.css file in your css folder.
Test the Application
Make sure in your XAMPP control panel Apache and MySQL services are running. Open the browser and run localhost/star_rating. You will see the below page displayed as below:
You can see the stars are showing with color for specific rating. If there is no rating it means rating is 0, then it displays Not-Rated and no star is colored.
Hope you could understand all the steps and you could develop and test it successfully.