Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PHP, SQL and HTML Modify the $dsn, $username, and $password to include your user

ID: 3904978 • Letter: P

Question

PHP, SQL and HTML

Modify the $dsn, $username, and $password to include your username where appropriate.

In the div element of the body, insert a table element with the needed Bootstrap classes to make it a bordered table that is striped.

Insert a caption element in the table, and include the text Employee List - followed by the current date. Use PHP to display the date in the format 02/22/2018 (with leading zeros for both the month and the day).

Insert the top row of the table, with table header cells for First Name, Last Name, Date of Birth, Age, Username, and Phone columns.

Use a PHP foreach loop to iterate through each row of the table, using these guidelines to help you display the data in the format described above:

Be sure to include comments in your code to describe the functions used to display the correct formats.

Use strpos() and substr() as needed to extract the first and last names. Since the first name comes after a comma and a space, ensure that the first name does not have a space before it when written to the table cell.

Display the date of birth using a 3-letter abbreviation for the month, the day of the month with a leading zero, a comma, and the 4-digit year. It may be easiest to set the date of birth as a DateTime object and then use ->format() to format the date appropriately.

To calculate the age, you can use date_diff() to find the difference in years between the date of birth and the current date. You can use date_create(date('Y-m-d')) for the current date, and ->format('%y') to format the result as a string that represents the age.

For the username, it should be the complete last name followed by the first character of the first name. Use an appropriate string function to make the username all lowercase.

The phone number is stored as a 10-digit number. Use string concatenation and the substr() function as needed to display the phone number in the (605) 256-5555 format.

<?php
$dsn = 'mysql:host=localhost;dbname=database_locos';
$username = 'database_locos';
$password = 'database_locos';

try {
    $db = new PDO($dsn, $username, $password);
} catch (PDOException $e) {
    $error_message = $e->getMessage();
    echo '<p>' . $error_message . '</p>';
}
$query = 'SELECT * FROM employees ORDER BY fullName';
$statement = $db->prepare($query);
$statement->execute();
$employees = $statement->fetchAll();
$statement->closeCursor();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Exercise</title>
    <link rel="stylesheet" href="bootstrap.min.css">
</head>
<body>
<div class="container table-responsive">

/************CODE GOES HERE*********************/

</div>
</body>
</html>

Explanation / Answer

If you have any doubts, please give me comment...

<?php

$dsn = 'mysql:host=localhost;dbname=database_locos';

$username = 'database_locos';

$password = 'database_locos';

try {

$db = new PDO($dsn, $username, $password);

} catch (PDOException $e) {

$error_message = $e->getMessage();

echo '<p>' . $error_message . '</p>';

}

$query = 'SELECT * FROM employees ORDER BY fullName';

$statement = $db->prepare($query);

$statement->execute();

$employees = $statement->fetchAll();

$statement->closeCursor();

?>

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>Exercise</title>

<link rel="stylesheet" href="bootstrap.min.css">

</head>

<body>

<div class="container table-responsive">

<table classs="table">

<thead>

<tr>

<th>First Name</th><th>Last Name</th><th>Date of Birth</th><th>Age</th><th>Username</th><th>Phone</th>

</tr>

</thead>

<tbody>

<?php

foreach($employees as $row => $emp){

$pos = strpos($emp['fullName'], ", ");

$firstName = substr($emp['fullName'],0, $pos);

$lastName = substr($emp['fullName'], $pos);

$age = date_diff(date_create(date("Y-m-d")) - date_create(date("Y-m-d", $row['dob'])))->format("%y");

$phone = $row['phone'];

$phone = "(".substr($phone, 0,3).") ".substr($phone, 4, 7)."-".substr($phone, 8);

echo "<tr><th>$firstName</th><th>$lastName</th><th>".date("M-d-Y", $emp['dob'])."</th><th>".$age."</th><th>".strtolower($row['username'])."</th><th>$phone</th></tr>";

}

?>

</tbody>

</table>

</div>

</body>

</html>