Webbserverprogrammering 1

Källkod

Följande filer och mappar finns under mappen webbserverprogrammering.
Mappar visas till vänster och filer till höger. Klicka på en fil eller mapp för att öppna nedan eller visa dess innehåll.

webbserverprogrammering/exercises/mysql/exercise_3/exercise_3_e/

7 filer

back_button.php
dbconnection.php
index.php
insert_values.php
print_table.php
reset.php
reset_garages_owners.php

print_table.php

146 lines ASCII Windows (CRLF)
<!DOCTYPE html>
<html lang="sv">
<head>
 <title>Show cars</title>
 <meta charset="utf-8">
 <style type="text/css">
  table.outputData {
   border-collapse: collapse;
   margin: 2em 3em;
   display: inline-block;
   vertical-align: top;
  }
  .outputData caption {
   margin-bottom: 1em;
  }
  .outputData th {
   font-weight: bold;
  }
  .outputData td, .outputData th {
   padding: 8px;
   border: 1px solid #686868;
  }
 </style>
</head>
<body>
 <?php
 
include('back_button.php');
 include (
'dbconnection.php');

 try {
  
$sql "SELECT * FROM garages";
  
$stmt $dbconn->prepare($sql);
  
$stmt->execute();

  
$output "
    <table class='outputData'>
     <caption>
      <strong>Garages</strong>
     </caption>
     <tr>
      <th>Index</th>
      <th>Name</th>
     </tr>
     "
;

  
$garages $stmt->fetchAll(PDO::FETCH_ASSOC PDO::FETCH_GROUP PDO::FETCH_UNIQUE);

  foreach (
$garages as $id => $garageInfo) {
   
$output .= "
    <tr>
     <td>"
.htmlentities($id)."</td>
     <td>"
.htmlentities($garageInfo['name'])."</td>
    </tr>"
;
  }

  
$output .= "</table>";
  echo 
$output;
 }
 catch(
PDOException $e) {
  echo 
$sql "<br>" $e->getMessage();
 }

 try {
  
$sql "SELECT * FROM owners ORDER BY name ASC";
  
$stmt $dbconn->prepare($sql);
  
$stmt->execute();

  
$output "
    <table class='outputData'>
     <caption>
      <strong>Owners</strong>
     </caption>
     <tr>
      <th>Index</th>
      <th>Name</th>
     </tr>
     "
;

  
$owners $stmt->fetchAll(PDO::FETCH_ASSOC  PDO::FETCH_GROUP PDO::FETCH_UNIQUE);

  foreach (
$owners as $id => $ownerInfo) {
   
$output .= "
    <tr>
     <td>"
.htmlentities($id)."</td>
     <td>"
.htmlentities($ownerInfo['name'])."</td>
    </tr>"
;
  }

  
$output .= "</table>";
  echo 
$output;
 }
 catch(
PDOException $e) {
  echo 
$sql "<br>" $e->getMessage();
 }


 try {
  
$sql "SELECT 
     cars.carId,
     cars.registrationNmbr,
     cars.color,
     garages.name AS garage,
     owners.name AS owner
    FROM cars 
    INNER JOIN garages ON cars.garage=garages.garageId 
    INNER JOIN owners ON cars.owner=owners.ownerId 
    ORDER BY garage ASC, owner ASC"
// "You can't use a column alias in ON or WHERE" but in ORDER BY works fine! :D
  
$stmt $dbconn->prepare($sql);
  
$stmt->execute();

  
$output "
    <table class='outputData'>
     <caption>
      <strong>Cars</strong>
     </caption>
     <tr>
      <th>Index</th>
      <th>Registration</th>
      <th>Color</th>
      <th>Garage</th>
      <th>Owner</th>
     </tr>
     "
;

  while (
$response $stmt->fetch(PDO::FETCH_ASSOC)) {
   
$output .= "<tr>
    <td>"
.htmlentities($response['carId'])."</td>
    <td>"
.htmlentities($response['registrationNmbr'])."</td>
    <td>"
.htmlentities($response['color'])."</td>
    <td>"
.htmlentities($response['garage'])."</td>
    <td>"
.htmlentities($response['owner'])."</td>
   </tr>"
;
  }
  
$output .= "</table>";
  echo 
$output;
 }
 catch(
PDOException $e) {
  echo 
$sql "<br>" $e->getMessage();
 }


 
$dbconn null;

 
?>
</body>
</html>