Advertisement
Guest User

Pehape

a guest
Apr 25th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3.     <head>
  4.         <meta charset="UTF-8">
  5.         <title>Ne petku ne, ja jsem v tom nevinne :(</title>
  6.     </head>
  7.     <body>
  8.         <?php
  9.         require_once 'trida.php';
  10.         $trida = new trida();
  11.         $skoly = $trida->selectAll();
  12.         foreach ($skoly as $skola) {
  13.             echo "<p>Nazev skoly: " . $skola['nazev'] . "<a href='detail.php?id=" . $skola['id'] . "'>Detail</a></p>";
  14.         }
  15.         ?>
  16.     </body>
  17. </html>
  18.  
  19. -------------------- TŘÍDA
  20.  
  21. <?php
  22.  
  23. class trida {
  24.     function __construct() {}
  25.  
  26.         function selectAll() {
  27.         $ukazVse = $this->connect()->prepare("SELECT * FROM `skoly`");
  28.         $ukazVse->execute();
  29.         return $ukazVse->fetchAll(PDO::FETCH_ASSOC);
  30.          
  31.     }
  32.  
  33.     function selectOne($id) {
  34.         $ukazJeden = $this->connect()->prepare("SELECT * FROM `skoly` WHERE `id` = " . $id . "");
  35.         $ukazJeden->execute();
  36.                 return $ukazJeden->fetchAll(PDO::FETCH_ASSOC);
  37.     }
  38.  
  39.     function connect() {
  40.         $dsn = "mysql:dbname=skola;host=localhost";
  41.         $user = "root";
  42.         $password = "";
  43.  
  44.         $conn = new PDO($dsn, $user, $password);
  45.         return $conn;
  46.     }
  47.  
  48. }
  49.  
  50. -------------------- DETAIL
  51.  
  52. <?php
  53.  
  54. require_once 'trida.php';
  55. $trida = new trida();
  56. if (isset($_GET["id"])) {
  57.     $id = $_GET["id"];
  58.     $sk = $trida->selectOne($id);
  59.  
  60.     foreach ($sk as $s) {
  61.         if ($s["typ"] = "z") {
  62.             $typ = "zakladni skola";
  63.         } else if ($s["typ"] = "s") {
  64.             $typ = "stredni skola";
  65.         } else if ($s["typ"] = "v") {
  66.             $typ = "vysoka skola";
  67.         }
  68.  
  69.         echo "<p>Nazev skoly: " . $s['nazev'] . " Typ skoly: " . $typ . " Pocet zaku: " . $s['pocetZaku'] . "</p>";
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement