Webbserverprogrammering 1

Show sourcecode

The following files exists in this folder. Click to view.

webbsrvprg/exercises/sql-intro/sql-intro3/

create-car.php
createtable.php
insertgarage-owner.php
sql-intro3-index.php
sql-intro3a.php
sql-intro3b.php
sql-intro3c.php
sql-intro3d.php

sql-intro3a.php

98 lines UTF-8 Windows (CRLF)
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    th{
      background-color:rgb(56, 56, 56);
      color: white;
    }
    h2{
      margin:0;
    }
    table{
      width:500px;
      border-radius:5px;
      border: solid 3px black;
    }
    td{
      width:150px;
    }
    th{
      border-radius:2px;
    }

  </style>
</head>
<body>
<?php

class TableRows extends RecursiveIteratorIterator {
  function 
__construct($it) {
    
parent::__construct($itself::LEAVES_ONLY);
  }

  function 
current(): string {
    return 
"<td>" parent::current(). "</td>";
  }

  function 
beginChildren(): void {
    echo 
"<tr>";
  }

  function 
endChildren(): void {
    echo 
"</tr>" "\n";
  }
}

include (
'../../../dbconnection.php');
try {
  echo 
"<table>";
  echo 
"<h2>Garage</h2>";
  echo 
"<tr><th>Id</th><th>Garage Namn</th></tr>";
  
$stmt $dbconn->prepare("SELECT garageid, name FROM garage");
  
$stmt->execute();

  
$result $stmt->setFetchMode(PDO::FETCH_ASSOC);
  foreach(new 
TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
    echo 
$v;
  }
  echo 
"<table>";
  echo 
"<br> <br>";
  echo 
"<h2>Ägare</h2>";
  echo 
"<tr><th>Id</th><th>Ägarens Namn</th></tr>";

  
$stmt $dbconn->prepare("SELECT ownerid, name FROM owner");
  
$stmt->execute();

  
$result $stmt->setFetchMode(PDO::FETCH_ASSOC);
  foreach(new 
TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
    echo 
$v;
  }

  echo 
"<table>";
  echo 
"<br> <br>";
  echo 
"<h2>Bilar</h2>";
  echo 
"<tr><th>Id</th><th>Regplåt</th><th>Färg</th><th>Garage Id</th><th>Ägar Id</th></tr>";

  
$stmt $dbconn->prepare("SELECT carid, regnr, color, garage, owner FROM car");
  
$stmt->execute();

  
$result $stmt->setFetchMode(PDO::FETCH_ASSOC);
  foreach(new 
TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
    echo 
$v;
  }
  
}
catch(
PDOException $e) {
  echo 
"Error:" $e->getMessage();
}

$dbconn null;

?>
</body>
</html>