Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //employee.php
- <?php
- include 'include/db_connection.php';
- include 'model/employee_model.php';
- $query = "SELECT * FROM Employees LIMIT 12";
- $res = $conn->query($query);
- $employees = array();
- foreach($res as $row){
- $newEmployees = new Employee();
- $newEmployees->setFirstName($row['FirstName']);
- $newEmployees->setLastName($row['LastName']);
- $newEmployees->setTitle($row['Title']);
- $newEmployees->setExtension($row['Extension']);
- $newEmployees->setBirthdate($row['BirthDate']);
- $newEmployees->setAddress($row['Address']);
- $newEmployees->setCity($row['City']);
- $newEmployees->setHomePhone($row['HomePhone']);
- $employees[] = $newEmployees;
- }
- include 'view/employee_view.php';
- ?>
- // employee_model.php
- <?php
- class Employee{
- private $firstName = "";
- private $lastName = "";
- private $title = "";
- private $extension = "";
- private $birthdate = "";
- private $address = "";
- private $city = "";
- private $homePhone = "";
- public function setFirstName($firstName){
- $this->firstName = $firstName;
- }
- public function getFirstName(){
- return $this->firstName;
- }
- public function setLastname($lastName){
- $this->lastName = $lastName;
- }
- public function getLastName(){
- return $this->lastName;
- }
- public function setTitle($title){
- $this->title = $title;
- }
- public function getTitle(){
- return $this->title;
- }
- public function setExtension($extension){
- $this->extension = $extension;
- }
- public function getExtension(){
- return $this->extension;
- }
- public function setBirthdate($birthdate){
- $this->birthdate = $birthdate;
- }
- public function getBirthdate(){
- return $this->birthdate;
- }
- public function setAddress($address){
- $this->address = $address;
- }
- public function getAddress(){
- return $this->address;
- }
- public function setCity($city){
- $this->city = $city;
- }
- public function getCity(){
- return $this->city;
- }
- public function setHomePhone($homePhone){
- $this->homePhone = $homePhone;
- }
- public function getHomePhone(){
- return $this->homePhone;
- }
- }
- ?>
- //employee_view.php
- <head>
- <title>Data Tables</title>
- <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
- <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.16/css/dataTables.bootstrap.min.css">
- <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
- <script type="text/javascript" src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
- <script type="text/javascript" src="https://cdn.datatables.net/1.10.16/js/dataTables.bootstrap.min.js"></script>
- </head>
- <style>
- td.details-control {
- background: url('../assets/details_open.png') no-repeat center center;
- cursor: pointer;
- }
- tr.shown td.details-control {
- background: url('../assets/details_close.png') no-repeat center center;
- }
- </style>
- <body>
- <header>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- <div class="navbar-header">
- <h4 stye="color:grey"> [IF635] Web Programming </h4>
- </div>
- <ul class="nav navbar-nav navbar-right">
- <li class="active"><a href="#">Employees</a></li>
- </ul>
- </div>
- </nav>
- </header>
- <div class="container col-5">
- <table id="example" class="table table-striped table-bordered" cellspacing="0">
- <thead>
- <tr>
- <th></th>
- <th>Last Name</th>
- <th>Title</th>
- <th>Extension</th>
- <th>Full Name</th>
- <th>Birth Date</th>
- <th>Address</th>
- <th>City</th>
- <th>Home Phone</th>
- </tr>
- </thead>
- <tfoot>
- <tr>
- <th></th>
- <th>Last Name</th>
- <th>Title</th>
- <th>Extension</th>
- <th>Full Name</th>
- <th>Birth Date</th>
- <th>Address</th>
- <th>City</th>
- <th>Home Phone</th>
- </tr>
- </tfoot>
- <tbody id="tbody">
- <?php
- foreach($employees as $row){
- echo "<tr>";
- echo "<td class=\"details-control\"></td>";
- echo "<td>" . $row->getLastName() . "</td>";
- echo "<td>" . $row->getTitle() . "</td>";
- echo "<td>" . $row->getExtension() . "</td>";
- echo "<td>" . $row->getFirstName() . " " . $row->getLastName() . "</td>";
- echo "<td>" . $row->getBirthdate() . "</td>";
- echo "<td>" . $row->getAddress() . "</td>";
- echo "<td>" . $row->getCity() . "</td>";
- echo "<td>" . $row->getHomePhone() . "</td>";
- echo "</tr>";
- }
- mysqli_free_result($res);
- mysqli_close($conn)
- ?>
- </tbody>
- </table>
- </div>
- </body>
- <script>
- function format ( d ) {
- // `d` is the original data object for the row
- return '<table id="example" class="table table-striped table-bordered" cellspacing="0">'+
- '<tr>'+
- '<td>Full Name:</td>'+
- '<td>'+d.fullName+'</td>'+
- '</tr>'+
- '<tr>'+
- '<td>Birth Date:</td>'+
- '<td>'+d.birthDate+'</td>'+
- '</tr>'+
- '<tr>'+
- '<td>Address</td>'+
- '<td>'+d.address+'</td>'+
- '</tr>'+
- '<tr>'+
- '<td>City</td>'+
- '<td>'+d.city+'</td>'+
- '</tr>'+
- '<tr>'+
- '<td>Home Phone</td>'+
- '<td>'+d.homePhone+'</td>'+
- '</tr>'+
- '</table>';
- }
- $(document).ready(function() {
- var table = $('#example').DataTable( {
- columnDefs: [
- { targets: [0, 1,2,3], visible: true},
- { targets: '_all', visible: false }
- ],
- "columns": [
- {
- "className": 'details-control',
- "orderable": false,
- "data": null,
- "defaultContent": ''
- },
- { "data": "lastName" },
- { "data": "title" },
- { "data": "extension" },
- { "data": "fullName" },
- { "data": "birthDate" },
- { "data": "address" },
- { "data": "city" },
- { "data": "homePhone" }
- ],
- "order": [[1, 'asc']]
- } );
- // Add event listener for opening and closing details
- $('#example tbody').on('click', 'td.details-control', function () {
- var tr = $(this).closest('tr');
- var row = table.row( tr );
- if ( row.child.isShown() ) {
- // This row is already open - close it
- row.child.hide();
- tr.removeClass('shown');
- }
- else {
- // Open this row
- row.child( format(row.data()) ).show();
- tr.addClass('shown');
- }
- } );
- } );
- </script>
- //db_connection.php
- <?php
- $host = "localhost";
- $username = "root";
- $dbname = "northwind";
- $password = "";
- $conn = new mysqli($host, $username, $password, $dbname);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement