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 '';                                 }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

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>'; 
                               }
                               
                         

Expert Solution
Step 1

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);
?>

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Complex Datatypes
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-engineering and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY