In: Computer Science
Database (SQL table data) ---please run the below commands in your database
Database create command
CREATE DATABASE myDB
Tables create command
CREATE TABLE `mydb`.`courses` ( `id` INT(11) NOT NULL
AUTO_INCREMENT , `course` VARCHAR(250) NOT NULL , `createdTime`
TIMESTAMP NOT NULL , PRIMARY KEY (`id`)) ENGINE = InnoDB;
CREATE TABLE `mydb`.`classes` ( `id` INT(11) NOT NULL
AUTO_INCREMENT , `classId` INT(11) NOT NULL , `name` VARCHAR(250)
NOT NULL , `createdTime` TIMESTAMP NOT NULL , PRIMARY KEY (`id`))
ENGINE = InnoDB;
Sample coures data command
INSERT INTO `courses` (`id`, `course`, `createdTime`) VALUES (NULL,
'HTML', CURRENT_TIMESTAMP), (NULL, 'CSS', CURRENT_TIMESTAMP);
INSERT INTO `courses` (`id`, `course`, `createdTime`) VALUES (NULL,
'JAVASCRIPT', CURRENT_TIMESTAMP), (NULL, 'PHP',
CURRENT_TIMESTAMP);
Sample classes data command
INSERT INTO `classes` (`id`, `classId`, `name`, `createdTime`)
VALUES (NULL, '1', 'Introduction', CURRENT_TIMESTAMP), (NULL, '1',
'HTML Tags', CURRENT_TIMESTAMP);
INSERT INTO `classes` (`id`, `classId`, `name`, `createdTime`)
VALUES (NULL, '1', 'Attributes', CURRENT_TIMESTAMP), (NULL, '1',
'HTML Forms', CURRENT_TIMESTAMP);
INSERT INTO `classes` (`id`, `classId`, `name`, `createdTime`)
VALUES (NULL, '2', 'Basics', CURRENT_TIMESTAMP), (NULL, '2', 'Style
Types', CURRENT_TIMESTAMP);
task.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password,
$dbname);
$coursesData='';
$classesData='';
//Get Courses
$sql = "SELECT id, course FROM courses";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$coursesData .= "<option
value='".$row['id']."'>".$row['course']."</option>";
}
}
//Get Classes
$sql1 = "SELECT name FROM classes where classId=1";
$result1 = mysqli_query($conn, $sql1);
if (mysqli_num_rows($result1) > 0) {
$index=1;
while($row1 = mysqli_fetch_assoc($result1)) {
$classesData .= "<tr>
<td>".$index++."</td>
<td>".$row1['name']."</td>
</tr>";
}
}
else
{
$classesData='NO data available';
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Courses</title>
<style>
table {
width:100%;
border-collapse: collapse;
}
table, th, td {
margin-top:10px;
padding:5px;
border: 1px solid black;
}
</style>
</head>
<body>
<div>
<label>Select
Course</label>
<select>
<?php echo
$coursesData ?>
</select>
</div>
<div>
<table>
<thead>
<tr>
<th>S.No</th>
<th>Classes</th>
</tr>
</thead>
<tbody>
<?php echo $classesData ?>
</tbody>
</table>
</div>
</body>
</html>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password,
$dbname);
$coursesData='';
$classesData='';
//Get Courses
$sql = "SELECT id, course FROM courses";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$coursesData .= "<option
value='".$row['id']."'>".$row['course']."</option>";
}
}
//Get Classes
$sql1 = "SELECT name FROM classes where classId=1";
$result1 = mysqli_query($conn, $sql1);
if (mysqli_num_rows($result1) > 0) {
$index=1;
while($row1 = mysqli_fetch_assoc($result1)) {
$classesData .= "<tr>
<td>".$index++."</td>
<td>".$row1['name']."</td>
</tr>";
}
}
else
{
$classesData='NO data available';
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Courses</title>
<style>
table {
width:100%;
border-collapse: collapse;
}
table, th, td {
margin-top:10px;
padding:5px;
border: 1px solid black;
}
</style>
</head>
<body>
<div>
<label>Select
Course</label>
<select>
<?php echo
$coursesData ?>
</select>
</div>
<div>
<table>
<thead>
<tr>
<th>S.No</th>
<th>Classes</th>
</tr>
</thead>
<tbody>
<?php echo $classesData ?>
</tbody>
</table>
</div>
</body>
</html>