PHP - Dynamically Switch Table
Submitted by razormist on Wednesday, October 30, 2019 - 19:38.
In this tutorial we will create a Dynamically Switch Table using MySQLi. This code will switch between the existing table in the MySQLi server when user click the confirm button. The code use a PHP POST method to call a binding ISSET function that will change the table content with the use of simple conditional statement if(). This is a user-friendly kind of program feel free user it in your program.
We will be using PHP as a scripting language that manage a database server to handle a bulk of data per transaction. It describe as an advance technology that manage both server and control-block of your machine.
student.php
teacher.php
There you have it we successfully created Dynamically Switch Table using MySQLi. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!
Getting Started:
First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.Creating Database
Open your database web server then create a database name in it db_table_switch, after that click Import then locate the database file inside the folder of the application then click ok.
Creating the database connection
Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.- <?php
- if(!$conn){
- }
- ?>
Creating The Interface
This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
- <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
- </div>
- </nav>
- <div class="col-md-3"></div>
- <div class="col-md-6 well">
- <h3 class="text-primary">PHP - Dynamically Switch Table</h3>
- <hr style="border-top:1px dotted #ccc;"/>
- <div class="col-md-3">
- <form method="POST" action="">
- <div class="form-group">
- <label>Select a table:</label>
- <select name="type" class="form-control">
- <option value="teacher">Teacher</option>
- <option value="student">Student</option>
- </select>
- </div>
- <center><button class="btn btn-primary" name="confirm">Confirm</button></center>
- </form>
- </div>
- <div class="col-md-9">
- <?php include'switch.php'?>
- </div>
- </div>
- </body>
- </html>
Creating the Main Function
This code contains the main function of the application. This code will change the table data when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as shown below. switch.php- <?php
- if($_POST['type'] == "student"){
- include'student.php';
- }else if($_POST['type'] == "teacher"){
- include'teacher.php';
- }
- }else{
- ?>
- <table class="table table-bordered">
- <thead class="alert-info">
- <tr>
- <th colspan="2">No Data</th>
- </tr>
- </thead>
- <tbody>
- <tr><td colspan="2">No Data</td></tr>
- </tbody>
- </table>
- <?php
- }
- ?>
- <table class="table table-bordered">
- <thead class="alert-info">
- <tr>
- <th>Student firstname</th>
- <th>Student lastname</th>
- </tr>
- </thead>
- <tbody>
- <?php
- require_once 'conn.php';
- echo "<tr>
- <td>".$fetch['firstname']."</td>
- <td>".$fetch['lastname']."</td>
- </tr>";
- }
- ?>
- </tbody>
- </table>
- <table class="table table-bordered">
- <thead class="alert-info">
- <tr>
- <th>Teacher firstname</th>
- <th>Teacher lastname</th>
- </tr>
- </thead>
- <tbody>
- <?php
- require_once 'conn.php';
- echo "<tr>
- <td>".$fetch['firstname']."</td>
- <td>".$fetch['lastname']."</td>
- </tr>";
- }
- ?>
- </tbody>
- </table>
Add new comment
- 239 views