I just wanted to ask, is it possible if I could arrange the echo records alphabetically? For example, I want to arrange the record based on the first letter of their name alphabetically. Also do I have to add something for that to happen? or is that even possible?
I know how to alphabetically arrange everything in HTML, I'm not sure if it works the same way as PHP.
here's my php code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<style>
body {
background-image: url("img/wood3.jpg");
}
html *
{
margin: auto;
color: #000 !important;
font-family: Questrial !important;
}
</style>
<link href="https://fonts.googleapis.com/css?family=Questrial" rel="stylesheet">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>CORE INTRANET</title>
</head>
<body>
<br>
<center><h1> View Records </h1></center>
<center><a href="home.php"><img src="img/homebutton.png" height="35" width="35"></a></center>
<br>
<?php
// connect to the database
include('connect-db.php');
// get the records from the database
if ($result = $mysqli->query("SELECT * FROM signup_and_login_users_table ORDER BY id"))
{
// display records if there are records to display
if ($result->num_rows > 0)
{
// display records in a table
echo "<table border='1' cellpadding='10'>";
// set table headers
echo "<tr><th>ID</th><th>Full Name</th><th>Username</th><th>Email</th><th>Address</th><th>Contact</th><th>Gender</th><th>Password</th><th>Access Level</th><th>Date</th></tr>";
while ($row = $result->fetch_object())
{
// set up a row for each record
echo "<tr>";
echo "<td>" . $row->id . "</td>";
echo "<td>" . $row->name . "</td>";
echo "<td>" . $row->username . "</td>";
echo "<td>" . $row->email . "</td>";
echo "<td>" . $row->address . "</td>";
echo "<td>" . $row->contact . "</td>";
echo "<td>" . $row->gender . "</td>";
echo "<td>" . $row->password . "</td>";
echo "<td>" . $row->user_levels . "</td>";
echo "<td>" . $row->date . "</td>";
echo "<td><a href='records.php?id=" . $row->id . "'>Edit</a></td>";
echo "<td><a href='delete.php?id=" . $row->id . "'>Delete</a></td>";
echo "</tr>";
}
echo "</table>";
}
// if there are no records in the database, display an alert message
else
{
echo "No results to display!";
}
}
// show an error if there is an issue with the database query
else
{
echo "Error: " . $mysqli->error;
}
// close database connection
$mysqli->close();
?>
</body>
</html>
</div>
Right now your SQL query reads:
SELECT * FROM signup_and_login_users_table ORDER BY id
This means that you sort the results by their numeric id. If you want to sort it by something else, then change the attribute. For example, if you want to sort it by the name:
SELECT * FROM signup_and_login_users_table ORDER BY name
Or descending order:
SELECT * FROM signup_and_login_users_table ORDER BY name DESC
You could also sort the results using PHP (with the conveniently named method sort
*), but it makes most sense to sort it in the query here.
I think the easiest way for you in this case is order from SQL actually, not PHP, so your query would become something like this: SELECT * FROM signup_and_login_users_table ORDER BY name DESC