Advertisement
Guest User

W06PEMWEB

a guest
Mar 21st, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.44 KB | None | 0 0
  1. //employee.php
  2. <?php
  3.     include 'include/db_connection.php';
  4.     include 'model/employee_model.php';
  5.  
  6.     $query = "SELECT * FROM Employees LIMIT 12";
  7.     $res = $conn->query($query);
  8.  
  9.     $employees = array();
  10.     foreach($res as $row){
  11.         $newEmployees = new Employee();
  12.  
  13.         $newEmployees->setFirstName($row['FirstName']);
  14.         $newEmployees->setLastName($row['LastName']);
  15.         $newEmployees->setTitle($row['Title']);
  16.         $newEmployees->setExtension($row['Extension']);
  17.         $newEmployees->setBirthdate($row['BirthDate']);
  18.         $newEmployees->setAddress($row['Address']);
  19.         $newEmployees->setCity($row['City']);
  20.         $newEmployees->setHomePhone($row['HomePhone']);
  21.  
  22.         $employees[] = $newEmployees;
  23.     }
  24.  
  25.     include 'view/employee_view.php';
  26. ?>
  27.  
  28. // employee_model.php
  29. <?php
  30.     class Employee{
  31.         private $firstName = "";
  32.         private $lastName = "";
  33.         private $title = "";
  34.         private $extension = "";
  35.         private $birthdate = "";
  36.         private $address = "";
  37.         private $city = "";
  38.         private $homePhone = "";
  39.  
  40.         public function setFirstName($firstName){
  41.             $this->firstName = $firstName;
  42.         }
  43.         public function getFirstName(){
  44.             return $this->firstName;
  45.         }
  46.  
  47.         public function setLastname($lastName){
  48.             $this->lastName = $lastName;
  49.         }
  50.         public function getLastName(){
  51.             return $this->lastName;
  52.         }
  53.  
  54.         public function setTitle($title){
  55.             $this->title = $title;
  56.         }
  57.         public function getTitle(){
  58.             return $this->title;
  59.         }
  60.  
  61.         public function setExtension($extension){
  62.             $this->extension = $extension;
  63.         }
  64.         public function getExtension(){
  65.             return $this->extension;
  66.         }
  67.  
  68.         public function setBirthdate($birthdate){
  69.             $this->birthdate = $birthdate;
  70.         }
  71.         public function getBirthdate(){
  72.             return $this->birthdate;
  73.         }
  74.  
  75.         public function setAddress($address){
  76.             $this->address = $address;
  77.         }
  78.         public function getAddress(){
  79.             return $this->address;
  80.         }
  81.  
  82.         public function setCity($city){
  83.             $this->city = $city;
  84.         }
  85.         public function getCity(){
  86.             return $this->city;
  87.         }
  88.  
  89.         public function setHomePhone($homePhone){
  90.             $this->homePhone = $homePhone;
  91.         }
  92.         public function getHomePhone(){
  93.             return $this->homePhone;
  94.         }
  95.     }
  96. ?>
  97.  
  98. //employee_view.php
  99. <head>
  100.     <title>Data Tables</title>
  101.     <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  102.     <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.16/css/dataTables.bootstrap.min.css">  
  103.    
  104.     <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
  105.     <script type="text/javascript" src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
  106.     <script type="text/javascript" src="https://cdn.datatables.net/1.10.16/js/dataTables.bootstrap.min.js"></script>
  107. </head>
  108. <style>
  109. td.details-control {
  110.     background: url('../assets/details_open.png') no-repeat center center;
  111.     cursor: pointer;
  112. }
  113. tr.shown td.details-control {
  114.     background: url('../assets/details_close.png') no-repeat center center;
  115. }
  116. </style>
  117. <body>
  118. <header>
  119.   <nav class="navbar navbar-default">
  120.     <div class="container-fluid">
  121.                 <div class="navbar-header">
  122.                   <h4 stye="color:grey"> [IF635] Web Programming </h4>
  123.       </div>
  124.       <ul class="nav navbar-nav navbar-right">
  125.         <li class="active"><a href="#">Employees</a></li>
  126.       </ul>
  127.     </div>
  128.   </nav>
  129. </header>
  130. <div class="container col-5">
  131. <table id="example" class="table table-striped table-bordered" cellspacing="0">
  132.         <thead>
  133.             <tr>
  134.                 <th></th>
  135.                 <th>Last Name</th>
  136.                 <th>Title</th>
  137.                 <th>Extension</th>
  138.                 <th>Full Name</th>
  139.                 <th>Birth Date</th>
  140.                 <th>Address</th>
  141.                 <th>City</th>
  142.                 <th>Home Phone</th>
  143.             </tr>
  144.         </thead>
  145.         <tfoot>
  146.             <tr>
  147.                 <th></th>
  148.                 <th>Last Name</th>
  149.                 <th>Title</th>
  150.                 <th>Extension</th>
  151.                 <th>Full Name</th>
  152.                 <th>Birth Date</th>
  153.                 <th>Address</th>
  154.                 <th>City</th>
  155.                 <th>Home Phone</th>
  156.             </tr>
  157.         </tfoot>
  158.         <tbody id="tbody">
  159.             <?php
  160.                 foreach($employees as $row){
  161.                   echo "<tr>";
  162.                   echo "<td class=\"details-control\"></td>";
  163.                   echo "<td>" . $row->getLastName() . "</td>";
  164.                   echo "<td>" . $row->getTitle() . "</td>";
  165.                   echo "<td>" . $row->getExtension() . "</td>";
  166.                   echo "<td>" . $row->getFirstName() . " " . $row->getLastName() . "</td>";
  167.                   echo "<td>" . $row->getBirthdate() . "</td>";
  168.                   echo "<td>" . $row->getAddress() . "</td>";
  169.                   echo "<td>" . $row->getCity() . "</td>";
  170.                   echo "<td>" . $row->getHomePhone() . "</td>";
  171.                   echo "</tr>";
  172.                 }
  173.  
  174.                 mysqli_free_result($res);
  175.                 mysqli_close($conn)
  176.             ?>
  177.         </tbody>
  178.     </table>
  179. </div>
  180. </body>
  181.  
  182. <script>
  183.   function format ( d ) {
  184.     // `d` is the original data object for the row
  185.     return '<table id="example" class="table table-striped table-bordered" cellspacing="0">'+
  186.         '<tr>'+
  187.             '<td>Full Name:</td>'+
  188.             '<td>'+d.fullName+'</td>'+
  189.         '</tr>'+
  190.         '<tr>'+
  191.             '<td>Birth Date:</td>'+
  192.             '<td>'+d.birthDate+'</td>'+
  193.         '</tr>'+
  194.         '<tr>'+
  195.             '<td>Address</td>'+
  196.             '<td>'+d.address+'</td>'+
  197.         '</tr>'+
  198.         '<tr>'+
  199.             '<td>City</td>'+
  200.             '<td>'+d.city+'</td>'+
  201.         '</tr>'+
  202.         '<tr>'+
  203.             '<td>Home Phone</td>'+
  204.             '<td>'+d.homePhone+'</td>'+
  205.         '</tr>'+
  206.     '</table>';
  207.   }
  208.  
  209.   $(document).ready(function() {
  210.     var table = $('#example').DataTable( {
  211.         columnDefs: [
  212.         { targets: [0, 1,2,3], visible: true},
  213.         { targets: '_all', visible: false }
  214.     ],
  215.         "columns": [
  216.             {
  217.                 "className":      'details-control',
  218.                 "orderable":      false,
  219.                 "data":           null,
  220.                 "defaultContent": ''
  221.             },
  222.             { "data": "lastName" },
  223.             { "data": "title" },
  224.             { "data": "extension" },
  225.             { "data": "fullName" },
  226.             { "data": "birthDate" },
  227.             { "data": "address" },
  228.             { "data": "city" },
  229.             { "data": "homePhone" }
  230.         ],
  231.         "order": [[1, 'asc']]
  232.     } );
  233.      
  234.     // Add event listener for opening and closing details
  235.     $('#example tbody').on('click', 'td.details-control', function () {
  236.         var tr = $(this).closest('tr');
  237.         var row = table.row( tr );
  238.  
  239.         if ( row.child.isShown() ) {
  240.             // This row is already open - close it
  241.             row.child.hide();
  242.             tr.removeClass('shown');
  243.         }
  244.         else {
  245.             // Open this row
  246.             row.child( format(row.data()) ).show();
  247.             tr.addClass('shown');
  248.         }
  249.     } );
  250. } );
  251. </script>
  252.  
  253. //db_connection.php
  254. <?php
  255.     $host = "localhost";
  256.     $username = "root";
  257.     $dbname = "northwind";
  258.     $password = "";
  259.  
  260.     $conn = new mysqli($host, $username, $password, $dbname);
  261. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement