PHP - Dynamically Switch Table

Submitted by: 
Language: 
Visitors have accessed this post 234 times.

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.

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.
tut1

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.

  1. <?php
  2. $conn = mysqli_connect("localhost", "root", "", "db_table_switch");
  3.  
  4. if(!$conn){
  5. die("Error: Failed to connect to database!");
  6. }
  7. ?>

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.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="<a href="https://sourcecodester.com">Sourcecodester</a>
  11. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Dynamically Switch Table</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <div class="col-md-3">
  19. <form method="POST" action="">
  20. <div class="form-group">
  21. <label>Select a table:</label>
  22. <select name="type" class="form-control">
  23. <option value="teacher">Teacher</option>
  24. <option value="student">Student</option>
  25. </select>
  26. </div>
  27. <center><button class="btn btn-primary" name="confirm">Confirm</button></center>
  28. </form>
  29. </div>
  30. <div class="col-md-9">
  31. <?php include'switch.php'?>
  32. </div>
  33. </div>
  34. </body>
  35. </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

  1. <?php
  2. if(ISSET($_POST['confirm'])){
  3. if($_POST['type'] == "student"){
  4. include'student.php';
  5. }else if($_POST['type'] == "teacher"){
  6. include'teacher.php';
  7. }
  8. }else{
  9. ?>
  10. <table class="table table-bordered">
  11. <thead class="alert-info">
  12. <tr>
  13. <th colspan="2">No Data</th>
  14. </tr>
  15. </thead>
  16. <tbody>
  17. <tr><td colspan="2">No Data</td></tr>
  18. </tbody>
  19. </table>
  20. <?php
  21. }
  22. ?>

student.php

  1. <table class="table table-bordered">
  2. <thead class="alert-info">
  3. <tr>
  4. <th>Student firstname</th>
  5. <th>Student lastname</th>
  6. </tr>
  7. </thead>
  8. <tbody>
  9. <?php
  10. require_once 'conn.php';
  11.  
  12. $query = mysqli_query($conn, "SELECT * FROM `student`") or die(mysqli_error());
  13. while($fetch = mysqli_fetch_array($query)){
  14. echo "<tr>
  15. <td>".$fetch['firstname']."</td>
  16. <td>".$fetch['lastname']."</td>
  17. </tr>";
  18. }
  19. ?>
  20. </tbody>
  21. </table>

teacher.php

  1. <table class="table table-bordered">
  2. <thead class="alert-info">
  3. <tr>
  4. <th>Teacher firstname</th>
  5. <th>Teacher lastname</th>
  6. </tr>
  7. </thead>
  8. <tbody>
  9. <?php
  10. require_once 'conn.php';
  11.  
  12. $query = mysqli_query($conn, "SELECT * FROM `teacher`") or die(mysqli_error());
  13. while($fetch = mysqli_fetch_array($query)){
  14. echo "<tr>
  15. <td>".$fetch['firstname']."</td>
  16. <td>".$fetch['lastname']."</td>
  17. </tr>";
  18. }
  19. ?>
  20. </tbody>
  21. </table>

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!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.