Advertisement
Guest User

Untitled

a guest
Jan 28th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.34 KB | None | 0 0
  1. <?php
  2. error_reporting(0);
  3. class database
  4. {
  5.     private $_connection = NULL;
  6.  
  7.     public function connect()
  8.     {
  9.         try
  10.         {
  11.             $this->_connection = new PDO('mysql:host=localhost; dbname=zadanie1; charset=utf8', 'root', '');
  12.         }
  13.         catch(PDOException $e)
  14.         {
  15.             echo "Błąd połączenia";
  16.         }
  17.     }
  18.  
  19.     public function query($q)
  20.     {
  21.         if(stristr($q, 'SELECT'))
  22.         {
  23.             $res = $this->_connection->query($q);
  24.             $res->execute();
  25.             $r = new resource();
  26.             return $r->fetch($res);
  27.         }
  28.  
  29.         if(stristr($q, 'DELETE'))
  30.         {
  31.             $delete = $this->_connection->query($q);
  32.             $delete->execute();
  33.         }
  34.  
  35.         if(stristr($q, 'UPDATE'))
  36.         {
  37.             $update = $this->_connection->query($q);
  38.             $update->execute();
  39.         }
  40.  
  41.         if(stristr($q, 'INSERT'))
  42.         {
  43.             $ins = $this->_connection->query($q);
  44.             $ins->execute();
  45.         }
  46.     }
  47. }
  48.  
  49. class resource
  50. {
  51.     private $_resource = NULL;
  52.  
  53.     public function res($r)
  54.     {
  55.         $this->_resource = $r;
  56.     }
  57.  
  58.     public function fetch($f)
  59.     {
  60.         return $f->fetch(PDO::FETCH_ASSOC);
  61.     }
  62. }
  63.  
  64. function table()
  65. {
  66.     $db = new database();
  67.     $db->connect();
  68.    
  69.     $c = $db->query('SELECT COUNT(Lp) AS Lp FROM Dane');
  70.     $count = $c['Lp'];
  71.    
  72.     for($i = 1; $i <= $count; $i++)
  73.     {
  74.         $row = $db->query("SELECT * FROM Dane WHERE Lp = $i");
  75.  
  76.         echo '<tr><td>' .$row['Lp']. '</td><td>' . $row['Name']. '</td><td>' . $row['Surname']. '</td><td>' . $row['Date']. '</td><td>' . $row['Number']. '</td><td>' . $row['Adres'].'</td></tr>';
  77.     }
  78. }
  79.  
  80. function updateLp($rem)
  81. {
  82.     $db = new database();
  83.     $db->connect();
  84.  
  85.     $c = $db->query('SELECT COUNT(Lp) AS Lp FROM Dane');
  86.     $count = $c['Lp'];
  87.    
  88.     if($db->query("SELECT Lp FROM Dane WHERE Lp = $rem+1") != "")
  89.     {
  90.         for($i = $rem+1; $i <= $count; $i++)
  91.         {
  92.             $db->query("UPDATE Dane SET Lp = Lp-1 WHERE Lp = $i");
  93.         }
  94.     }
  95. }
  96.  
  97. function remove()
  98. {
  99.     $remove = $_GET['rem'];
  100.     $db = new database();
  101.     $db->connect();
  102.  
  103.     if($db->query("SELECT Lp FROM Dane WHERE Lp = $remove") != "")
  104.     {
  105.         $db->query("DELETE FROM Dane WHERE Lp = $remove");
  106.         updateLp($remove);
  107.     }
  108. }
  109.  
  110. function insert()
  111. {
  112.     $db = new database();
  113.     $db->connect();
  114.  
  115.     $name = $_GET["name"];
  116.  
  117.     $db->query("INSERT INTO Dane (Name) VALUES ('$name')");
  118.     $_GET["name"] = "";
  119.  
  120. }
  121.  
  122. function prepare()
  123. {
  124.     if($_GET["rem"] != "")
  125.     {
  126.         remove();
  127.     }
  128.     if($_GET["name"] != "")
  129.     {
  130.         insert();
  131.         $_GET["name"] = "";
  132.     }
  133. }
  134. ?>
  135.  
  136. <!DOCTYPE html>
  137. <html>
  138.     <head>
  139.         <meta charset="utf-8" />
  140.         <title>Zadanie 1</title>
  141.         <style>
  142.             body
  143.             {
  144.                 margin: 0;
  145.                 padding: 0;
  146.                 background: lightgrey;
  147.             }
  148.  
  149.             td
  150.             {
  151.                 text-align: center;
  152.                 width: 200px;
  153.                 height: 20px;
  154.             }
  155.  
  156.             th
  157.             {
  158.                 text-align: center;
  159.                 width: 200px;
  160.                 height: 20px;
  161.             }
  162.  
  163.             .divtable
  164.             {
  165.                 width: 1200px;
  166.                 min-height: 200px;
  167.                 margin-left: auto;
  168.                 margin-right: auto;
  169.                 margin-top: 10px;
  170.             }
  171.  
  172.             .divremove
  173.             {
  174.                 width: 50%;
  175.                 height: 50%;
  176.                 text-align: center;
  177.                 float: left;
  178.                 margin-top: 100px;
  179.             }
  180.             .divadd
  181.             {
  182.                 width: 50%;
  183.                 height: 100%;
  184.                 text-align: center;
  185.                 float: right;
  186.             }
  187.  
  188.             form
  189.             {
  190.                 width: 400px;
  191.                 height: 50%;
  192.                 border-style: solid;
  193.                 border-color: grey;
  194.                 margin-left: auto;
  195.                 margin-right: auto;
  196.                 border-radius: 20px 20px 20px 20px;
  197.                 padding: 5px 5px 100px 5px;
  198.             }
  199.  
  200.             p
  201.             {
  202.                 width: 300px;
  203.                 height: 40px;
  204.                 margin-left: auto;
  205.                 margin-right: auto;
  206.                 font-size: 20px;
  207.             }
  208.  
  209.             input
  210.             {
  211.                 float: right;
  212.                 height: 35px;
  213.                 border-radius: 20px 20px 20px 20px;
  214.                 border-style: none;
  215.             }
  216.  
  217.             .button
  218.             {
  219.                 width: 100px;
  220.                 height: 50px;
  221.                 margin-right: 150px;
  222.                 margin-top: 20px;
  223.                 font-size: 24px;
  224.                 border-radius: 20px 20px 20px 20px;
  225.             }
  226.  
  227.  
  228.         </style>
  229.     </head>
  230.     <body>
  231.         <?php prepare() ?>
  232.         <div class="divtable">
  233.             <table>
  234.                 <tr>
  235.                     <th>Lp</th>
  236.                     <th>Imię</th>
  237.                     <th>Nazwisko</th>
  238.                     <th>Data urodzenia</th>
  239.                     <th>Numer telefonu</th>
  240.                     <th>Adres zamieszkania</th>
  241.                 </tr>
  242.                 <?php table(); ?>
  243.             </table>
  244.         </div>
  245.         <div class="divremove">
  246.             <form metod ="get" action="index.php">
  247.                 <h1>Usuwanie z tabeli</h1>
  248.             <p>Nr Lp. <input type="number" name="rem"/></p>
  249.             <input type="submit" value="Usuń" name="usun" class="button"/>
  250.             </form>
  251.         </div>
  252.         <div class="divadd">
  253.             <form metod="get" action="index.php">
  254.                 <h1>Dodawanie do tabali</h1>
  255.                 <p>Imię: <input type="text" name="name" /></p>
  256.                 <p>Nazwisko <input type="text" name="surname" /></p>
  257.                 <p>Data urodzenia <input type="date" name="date"/></p>
  258.                 <p>Numer telefonu <input type="number" name="number"/></p>
  259.                 <p style="width:350px;">Adres zamieszkania <input type="text" name="adres" /></p>
  260.                 <input type="submit" value="Dodaj" name="add" class ="button" />
  261.         </form>
  262.         </div>
  263.     </body>
  264. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement