Last active
September 3, 2024 07:08
-
-
Save quickgrid/35d3bcc75724aca3f889153c3ef7afff to your computer and use it in GitHub Desktop.
Pulls data from mysql database and show in beautiful bootstrap table.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<title>Sample PHP Database Application</title> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.5/css/bootstrap.min.css"> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.5/js/bootstrap.min.js"></script> | |
</head> | |
<body> | |
<?php | |
//Change the password to match your configuration | |
$link = mysqli_connect("localhost", "root", "yourPassword", "cse"); | |
// Check connection | |
if($link === false){ | |
die("ERROR: Could not connect. " . mysqli_connect_error()); | |
} | |
echo "<br>"; | |
$sql = "SELECT id, name, salary FROM instructor"; | |
$result = $link->query($sql); | |
echo "<div class='container'>"; | |
echo "<div class='row-fluid'>"; | |
echo "<div class='col-xs-6'>"; | |
echo "<div class='table-responsive'>"; | |
echo "<table class='table table-hover table-inverse'>"; | |
echo "<tr>"; | |
echo "<th>ID</th>"; | |
echo "<th>Name</th>"; | |
echo "<th>Salary</th>"; | |
echo "</tr>"; | |
if ($result->num_rows > 0) { | |
// output data of each row | |
while($row = $result->fetch_assoc()) { | |
echo "<tr>"; | |
echo "<td>" . $row["id"] . "</td>"; | |
echo "<td>" . $row["name"] . "</td>"; | |
echo "<td>" . $row["salary"] . "</td>"; | |
echo "</tr>"; | |
} | |
} else { | |
echo "0 results"; | |
} | |
echo "</table>"; | |
$sql = "SELECT semester, year FROM section"; | |
$result = $link->query($sql); | |
echo "<table class='table table-hover table-inverse'>"; | |
echo "<tr>"; | |
echo "<th>semester</th>"; | |
echo "<th>year</th>"; | |
echo "</tr>"; | |
if ($result->num_rows > 0) { | |
// output data of each row | |
while($row = $result->fetch_assoc()) { | |
echo "<tr>"; | |
echo "<td>" . $row["semester"] . "</td>"; | |
echo "<td>" . $row["year"] . "</td>"; | |
echo "</tr>"; | |
} | |
} else { | |
echo "0 results"; | |
} | |
echo "</table>"; | |
echo "</div>"; | |
echo "</div>"; | |
/* | |
* second table | |
*/ | |
$sql = "SELECT course_id, title, dept_name, credits FROM course"; | |
$result = $link->query($sql); | |
echo "<div class='col-xs-6'>"; | |
echo "<div class='table-responsive'>"; | |
echo "<table class='table table-hover table-inverse'>"; | |
echo "<tr>"; | |
echo "<th>Course ID</th>"; | |
echo "<th>Title</th>"; | |
echo "<th>Department</th>"; | |
echo "<th>credits</th>"; | |
echo "</tr>"; | |
if ($result->num_rows > 0) { | |
// output data of each row | |
while($row = $result->fetch_assoc()) { | |
echo "<tr>"; | |
echo "<td>" . $row["course_id"] . "</td>"; | |
echo "<td>" . $row["title"] . "</td>"; | |
echo "<td>" . $row["dept_name"] . "</td>"; | |
echo "<td>" . $row["credits"] . "</td>"; | |
echo "</tr>"; | |
} | |
} else { | |
echo "0 results"; | |
} | |
echo "</table>"; | |
echo "</div>"; | |
echo "</div>"; | |
echo "</div>"; | |
/* | |
* second row | |
*/ | |
$sql = "SELECT building, room_number, time_slot_id FROM section"; | |
$result = $link->query($sql); | |
echo "<div class='row-fluid'>"; | |
echo "<div class='col-xs-6'>"; | |
echo "<div class='table-responsive'>"; | |
echo "<table class='table table-hover table-inverse'>"; | |
echo "<tr>"; | |
echo "<th>building</th>"; | |
echo "<th>room_number</th>"; | |
echo "<th>time_slot_id</th>"; | |
echo "</tr>"; | |
if ($result->num_rows > 0) { | |
// output data of each row | |
while($row = $result->fetch_assoc()) { | |
echo "<tr>"; | |
echo "<td>" . $row["building"] . "</td>"; | |
echo "<td>" . $row["room_number"] . "</td>"; | |
echo "<td>" . $row["time_slot_id"] . "</td>"; | |
echo "</tr>"; | |
} | |
} else { | |
echo "0 results"; | |
} | |
echo "</table>"; | |
echo "</div>"; | |
echo "</div>"; | |
/* | |
* second table | |
*/ | |
$sql = "SELECT course_id, prereq_id FROM prereq"; | |
$result = $link->query($sql); | |
echo "<div class='col-xs-6'>"; | |
echo "<div class='table-responsive'>"; | |
echo "<table class='table table-hover table-inverse'>"; | |
echo "<tr>"; | |
echo "<th>Course ID</th>"; | |
echo "<th>Prerequite ID</th>"; | |
echo "</tr>"; | |
if ($result->num_rows > 0) { | |
// output data of each row | |
while($row = $result->fetch_assoc()) { | |
echo "<tr>"; | |
echo "<td>" . $row["course_id"] . "</td>"; | |
echo "<td>" . $row["prereq_id"] . "</td>"; | |
echo "</tr>"; | |
} | |
} else { | |
echo "0 results"; | |
} | |
echo "</table>"; | |
echo "</div>"; | |
echo "</div>"; | |
echo "</div>"; | |
echo "</div>"; | |
// Close connection | |
mysqli_close($link); | |
?> | |
</body> | |
</html> |
This will create the tables based on his code provided.
CREATE TABLE instructor (
id int,
name varchar(255),
salary varchar(255)
);
CREATE TABLE section (
id int,
semester varchar(255),
year varchar(255),
building varchar(255),
room_number int,
time_slot_id int
);
CREATE TABLE course (
id int,
course_id int,
title varchar(255),
dept_name varchar(255),
credits int
);
CREATE TABLE prereq (
id int,
course_id int,
prereq_id int
);
You made my day.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
But where is the database.sql ???