How would I programmatically loop through employees ( a SQL Table) and display each name as element and sort them by LastName? Example: foreach ($employees as $emp) { echo ''; echo $emp['FirstName']; echo ''; }
How would I programmatically loop through employees ( a SQL Table) and display each name as <li> element and sort them by LastName?
Example:
foreach ($employees as $emp) {
echo '<li class="mdl-list__item">';
echo $emp['FirstName'];
echo '</li>';
}
Required source code :-
Solution :- i am writing the required php code where we fetch the detail of table - Employees(name of employees order by LastName) and print it in <li> list of html content.....
--------------------------------------------------------------------------------------
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// this sql query gives the name of employees in sorted order by last name
$sql = "SELECT Firstname, Lastname FROM Employees ORDER BY Lastname";
$employees = mysqli_query($conn, $sql);
if (mysqli_num_rows($employees) > 0) {
// output data of each row
foreach ($employees as $emp) {
echo "<li class="mdl-list__item">";
echo $emp['FirstName'];
echo $emp['LastName'];
echo "</li>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
Step by step
Solved in 2 steps with 1 images