Today i will share how to display data from MySQL database into HTML table using PHP. I will also show that how to store values from database table into HTML array, and how to handle that array in PHP. For this purpose first i will create a database with name allphptricks and then create a table in database with name sports and dump dummy data into it.
Steps to Display Data From Database Into HTML Table Using PHP
- Create a Database
- Create a Database Table
- Dumping Data into Table
- Create a Database Connection Page
- Create a Main Index Page
1. Create a Database
Execute the following query in your MySQL query.
2. Create a Database Table
Run the following query in your above database.
3. Dumping Data into Table
Now i will insert sample data in our table, following query is dumping data into table.
For your ease i have also attached an sport.sql file in this tutorial download, you can import that file if you do not want to create table and dumping data.
4. Create a Database Connection Page
Create a database connection page with name db.php and copy the following code in it.
5. Create a Main Index Page
Now create a main index.php page which contain the actual script of PHP which will show data from database into HTML table and also it will store all values in HTML array and on form submission, it will catch all selected values and print them on web page, you can also store it in your database.
Now copy the below code in your index.php page right after start of <body> tag
HTML Form & PHP Loop Script
In the input field name i used sports[] these square brackets will make it HTML array. The above code will get all sports name from database table and view them in the HTML table form, each row display three sports name. But this will also need database connection, so copy the below code before the start of <html> tag in the page header.
DB Connection & PHP Loop Script of Array
Above code will not only include database connection file but it will also check if form is submitted so it will check array variable, if it is array then it will start foreach loop to catch all selected values by user and store it in $status variable which will display result below HTML table.
Little CSS of HTML Table
Add following CSS in your index.php before closing </head>
If you found this tutorial helpful, share it with your friends and developers group.
No comments:
Post a Comment