So... I have a fully working code, but my boss asked me to add a button that downloads the table in excel format with the values from the database and I was like, I can do that?
I also asked chatGPT but it has a character limit so it never finished the code, sad.
I am still in school and had to learn PHP and MySQL from the scratch in the last 4 weeks, at least I knew a bit of HTML before.
//code
<?php
$host = "localhost";
$username = "root";
$password = "";
$dbname = "esercizi3";
$con = mysqli_connect($host, $username, $password, $dbname);
if (!$con)
{
die("Connection failed!" . mysqli_connect_error());
}
$sql = "SELECT
*
FROM
courses";
$result = $con->query($sql);
?>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<?php
if(!isset($_POST['delete']) && !isset($_POST['download']))
{
?>
<div style="width: 650px">
<table class = "table table-bordered">
<thead>
<tr>
<td class="text-center" colspan="8">
<button type="button" name="download" class="btn btn-dark" style="height: 100%; width:33%;">Download Excel</button>
<a href="listStudent.php"><button type="button" class="btn btn-secondary" style="height: 100%; width:32%;">Students' List</button></a>
<?php //button ?> <a href="addCourse.php"><button type="button" class="btn btn-primary" style="height: 100%; width:33%;">Add Course</button></a>
</td>
</tr>
<th class="text-center" colspan="8"><h2>Courses' List</h2></th>
<tr>
<th class="text-center" scope="col">ID</th>
<th class="text-center" scope="col">Name</th>
<th class="text-center" scope="col">Students included</th>
<th class="text-center" class="text-center" colspan="2">Actions</th>
</tr>
</thead>
<tbody>
<?php
while($row = $result->fetch_assoc())
{
$ID_c = $row["ID_c"];
?>
<tr>
<td class="text-center"><?php echo $row["ID_c"]; ?></td>
<td class="text-center"><?php echo $row["name_c"]; ?></td>
<?php
$sql2 = "SELECT
*
FROM
studentsCourses
WHERE
ID_course_sc = $ID_c";
$result2 = $con->query($sql2);
?>
<td class="text-center">
<?php
echo "<ul>";
while($row2 = $result2->fetch_assoc())
{
$ID_student = $row2["ID_student_sc"];
$sql3 = "SELECT
*
FROM
students
WHERE
ID = $ID_student";
$result3 = $con->query($sql3);
$row3 = $result3->fetch_assoc();
echo "<li>" .$row3["f_name"]. " " .$row3["l_name"]. "</li>";
}
echo "</ul>";
?>
</td>
<td class="text-center">
<form action="editCourse.php" method="POST">
<input type="hidden" id="submit" name="ID_c" value="<?php echo $row['ID_c'];?>">
<button type="submit" class="btn btn-success" style="height: 100%; width:100%;">EDIT</button>
</form>
</td>
<td class="text-center">
<form method="POST">
<input type="hidden" id="ID_c" name="ID_c" value="<?php echo $row['ID_c'];?>">
<button name="delete" type="submit" class="btn btn-danger" style="height: 100%; width:100%;">DELETE</button>
</form>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<?php
}else
{
$ID_c = $_POST['ID_c'];
$sql = "
DELETE FROM
courses
WHERE
ID_c = $ID_c";
if ($con->query($sql) === TRUE) {
//entry deleted
} else {
//error
}
$con->close();
header('Location: http://localhost/esercizi3/listCourse.php');
}
?>
</body>
</html>