Util.php
<?php function print_table($data, $headers) { # print_r($data); echo "<table class='table table-striped'>"; echo "<tr>"; foreach ($headers as $cell) { echo "<th>$cell</th>"; } echo "</tr>"; foreach ($data as $rows) { echo "<tr>"; foreach ($rows as $cell) { echo "<td>$cell</td>"; } echo "</tr>"; } echo "</table>"; }
<html> <head> <link rel="stylesheet" type="text/css" href="bootstrap/dist/css/bootstrap.min.css"> </head> <h2>Employees</h2> <?php require_once('Util.php'); $db = oci_connect('hr', 'hr', 'kurs2.wifi'); if (empty($_GET)) { $sql = "select department_id id, department_name from departments where department_id in (select department_id from employees)"; $q = oci_parse($db, $sql); $r = oci_execute($q, OCI_DEFAULT); $table = array(); while ($row = oci_fetch_array($q, OCI_ASSOC)) { $table[] = $row; } ?> <form class="form-horizontal" role="form" action='employees_departments.php' method='get'> <div class="form-group"> <select class="col-sm-offset-2 col-sm-3" name='department'> <?php foreach ($table as $dep) { printf("<option value='%s'>%s</option>\n", $dep['ID'], $dep['DEPARTMENT_NAME']); } ?> </select> </div> <div class="col-sm-offset-2 col-sm-10"> <input type="submit" value="Suchen" class="btn"/> </div> </form> <?php } else { $department_id = $_GET['department']; $sql = "select first_name, last_name, salary, email, hire_date from employees where department_id = :b_department_id"; $q = oci_parse($db, $sql); oci_bind_by_name($q, ":b_department_id", $department_id ); $r = oci_execute($q, OCI_DEFAULT); $table = array(); while ($row = oci_fetch_array($q, OCI_NUM)) { $table[] = $row; } $headers = array('First Name', 'Last Name', 'Salary', 'Email', 'Hire Date'); print_table($table, $headers); } ?> </html>
<html> <head> <link rel="stylesheet" type="text/css" href="bootstrap/dist/css/bootstrap.min.css"> <script src="jquery-2.1.1.min.js"></script> </head> <?php require_once('Config.php'); require_once('Util.php'); $db = oci_connect('hr', 'hr', 'kurs2.wifi'); if (empty($_GET)) { $sql = "select department_id, department_name from departments where department_id in (select department_id from employees)"; $q = oci_parse($db, $sql); $r = oci_execute($q, OCI_DEFAULT); $table = array(); while ($row = oci_fetch_array($q, OCI_NUM)) { $table[] = $row; } ?> <script> $(function () { $( "#depselect" ).change(function() { $.get("http://localhost/employees_departments_js.php?department="+ $( this ).val(), function( data ) { $("#output").html( data ); }); }); }); </script> <h2>Select Department</h2> <div class="form-group"> <select id='depselect' class="col-sm-offset-2 col-sm-3" name='department'> <?php foreach ($table as $dep) { printf("<option value='%s'>%s</option>\n", $dep[0], $dep[1]); } ?> </select> <br> <div id='output'> </div> </form> <?php } else { $department_id = $_GET['department']; $sql = "select first_name, last_name, salary, email, hire_date from employees where department_id = :b_department_id"; $q = oci_parse($db, $sql); oci_bind_by_name($q, ":b_department_id", $department_id ); $r = oci_execute($q, OCI_DEFAULT); $table = array(); while ($row = oci_fetch_array($q, OCI_NUM)) { $table[] = $row; } $headers = array('First Name', 'Last Name', 'Salary', 'Email', 'Hire Date'); print_table($table, $headers); } ?> </html>