I have a php table that displays the data within a mysql table.
Now the table is already in alphabetical order by Department but the users within in each Department are not.
In the example below you will see that the departments are in alphabetical order but the user data under each department is not. Eg.
Admin Department
Wade 3234234 [email protected]
George 3434323 [email protected]
Workshop Department
Zyiad 45454523 [email protected]
Buffy 48282191 [email protected]
My code:
<center>
<html>
<head>
<title>Extension List</title></head><body>
<br>
<h1> Alpine Motors Extension List</h1>
<h2><a href="add.php">Add Extension</a>
<br>
<br>
<form action="AlpineExtensionList.php" method='post'>
<input type="submit" value="Print PDF"/>
<br>
<br>
<a href="delete.php"> Delete Extension</a></h2>
<br>
<br>
<?php
$db_host = 'localhost';
$db_user = 'root';
$db_pwd = '*****';
$database = 'list';
$table = 'users';
$conn = mysqli_connect($db_host, $db_user, $db_pwd) or die("Connecting to database failed");
mysqli_select_db($conn, $database) or die("Can't select database");
// sending query
$result = mysqli_query($conn, "SELECT name, email, extension, phone, department FROM {$table} ORDER BY department ASC");
if (!$result) {
die("Query to show fields from table failed");
}
echo "<table border='1'><tr>";
// printing table rows
$temp = "";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
if ($row['department'] != $temp){
echo "<td colspan='4' style='text-align: center; font-weight: bold'>{$row['department']}</td></tr>\n<tr>";
$temp = $row['department'];
}
echo "<td>" . $row['name'] . "</td><td>" . $row['email'] . "</td><td>" . $row['extension'] . "</td><td>" . $row['phone'] . "</td>";
echo "</tr>\n";
}
// sending query
$result = mysqli_query($conn, "SELECT name, email, extension, phone, department FROM {$table} ORDER BY department ASC");
if (!$result) {
die("Query to show fields from table failed");
}
echo "<table border='1'><tr>";
// printing table rows
$temp = "";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
if ($row['department'] != $temp){
echo "<td colspan='4' style='text-align: center; font-weight: bold'>{$row['department']}</td></tr>\n<tr>";
$temp = $row['department'];
}
echo "<td>" . $row['name'] . "</td><td>" . $row['email'] . "</td><td>" . $row['extension'] . "</td><td>" . $row['phone'] . "</td>";
echo "</tr>\n";
}
mysqli_free_result($result);
echo "</table>"
?>
</h2>
</body>
</html>
</center>